my class is
public class Usage {
public static void updateAccount(Usage__c[] usageLogs) {
for (Usage__c u :usageLogs) {
Account myAccount = [SELECT Id, ASEO_Last_Access__c FROM Account WHERE ASEO_Company_Id__c = :u.Company_Id__c];
myAccount.ASEO_Last_Access__c = datetime.now();
update myAccount;
u.Account__c = myAccount.Id;
}
}
}
and trigger
trigger triger on Compnay__c(before insert){
Company__c [] compny= tigger.new
updateAccount(compny)
}
getting error
Attribution to: Riaz Muhammad
Possible Suggestion/Solution #1
updateAccount
is a static method, so you have to prepend it's name with class name when calling (like in Java):
trigger triger on Company__c(before insert){
Company__c [] compny= trigger.new;
Usage.updateAccount(compny);
}
Attribution to: IvanR
Possible Suggestion/Solution #2
Theres a lot wrong with that code:
- The trigger should be firing on
Company__c
notCompnay__c
- All lines of code need to be closed with a
;
symbol. egCompany__c [] compny= trigger.new;
- Its
trigger.new
nottigger.new
- You are not calling the
updateAccount
method correctly. It should beUsage.updateAccount(compny);
Usage.updateAccount
is expecting a List ofUsage__c
objects not a List ofCompany__c
objects.- Your trigger is firing
before insert
(ie before an Id is assigned to the Company record) but your method is doing a query by Company Ids which will not find the newly inserted objects - You're doing a SOQL call in a
for loop
which should be avoided as you will hit governor limits very quickly updateAccount
is changing a field onUsage__c
(u.Account__c = myAccount.Id;
) but you don't do an update on the object so the change never gets persisted
Your trigger is probably supposed to look something like:
trigger meaningfulName on Company__c(after insert){
List<Usage__c> usages = new List<Usage__c>();
for (Company__c c : Tigger.new) {
usages.add(new Usage(c)); // Assuming that there is a constructor for Usage
}
if (usages.size() > 0) {
Usage.updateAccount(usages);
}
}
Attribution to: BarCotter
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34082