Find your content:

Search form

You are here

how to call class method in apex

 
Share

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 not Compnay__c
  • All lines of code need to be closed with a ; symbol. eg Company__c [] compny= trigger.new;
  • Its trigger.new not tigger.new
  • You are not calling the updateAccount method correctly. It should be Usage.updateAccount(compny);
  • Usage.updateAccount is expecting a List of Usage__c objects not a List of Company__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 on Usage__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

My Block Status

My Block Content