function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
force shahidforce shahid 

How to call different methods on same event in one trigger

Hi Friends,

I written one apex class with different methods. now i call this apex class in one trigger. But It gives some error.

Apex class :

public with sharing class Account_Handler {
 public static void afterInsert (List<Opportunity> newList){
        List<Account> Updacc = new List<Account>();
      for(Opportunity opp : newList){
              List<Account> acc = [select id,Total_Open_Opportunities__c from Account where Id =: opp.AccountId]; 
              List<Opportunity > opportunity= [select id,AccountId,StageName from Opportunity where AccountId =:opp.AccountId AND ( StageName NOT IN ('Closed Won','Closed Lost')) ];
           for(Account a :acc )
           {
             a.Total_Open_Opportunities__c=opportunity.size();
             Updacc.add(a);
          }
      }update Updacc; 
   }
    
    public static void afterUpdate (List<Opportunity> newList){
          List<Account> Updacc = new List<Account>();
        for(Opportunity opp : newList){
              List<Account> acc = [select id,Total_Open_Opportunities__c from Account where Id =: opp.AccountId]; 
              List<Opportunity > opportunity= [select id,AccountId,StageName from Opportunity where AccountId =:opp.AccountId AND ( StageName NOT IN ('Closed Won','Closed Lost'))];
           for(Account a :acc )
           {
             a.Total_Open_Opportunities__c=opportunity.size();
             Updacc.add(a);
           }
       }update Updacc; 
    } 
    
    public static void afterDelete (List<Opportunity> oldList){
          List<Account> Updacc = new List<Account>();
        for(Opportunity opp : oldList){
              List<Account> acc = [select id,Total_Open_Opportunities__c from Account where Id =: opp.AccountId]; 
              List<Opportunity > opportunity= [select id,AccountId,StageName from Opportunity where AccountId =:opp.AccountId AND ( StageName NOT IN ('Closed Won','Closed Lost'))];
           for(Account a :acc )
           {
             a.Total_Open_Opportunities__c=opportunity.size();
             Updacc.add(a);
           }
       }update Updacc; 
    }   
}

This is Sample code. Its not a complete code. I have another methods also.

Trigger :

trigger AccountTrigger on Account (before insert, after insert, after update,after delete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {
      Account_Handler.beforeInsert(Trigger.new);
    }
  }

  if (Trigger.isAfter) {
    if (Trigger.isInsert) {
      Account_Handler.afterInsert(Trigger.new);
      Account_Handler.afterInsert(Trigger.new);
    }
    if (Trigger.isUpdate) {
        Account_Handler.afterUpdate(Trigger.new);
      Account_Handler.afterUpdate(Trigger.newMap, Trigger.oldMap);
      
    }
    if (Trigger.isDelete) {
      Account_Handler.afterDelete(Trigger.old);
    }
  }
}

Error :

Method does not exist or incorrect signature: Account_Handler.afterUpdate(List<Account>)

This error comes for afterUpdate & afterdelete methods.

Thanks in advance,
Shahid.
Best Answer chosen by force shahid
Shweta_AgarwalShweta_Agarwal
Hi Shahid,

In you class in method parameter you are using List<Opportunity> and you have written trigger on Account object So it will pass List<account> in afterupdate method.

public static void afterUpdate (List<Opportunity> newList){}

So if want use same class in your account trigger then create a method with list<Account>

public static void afterUpdate (List<Account> newList){}

Hope this will help you

Thanks
Shweta

All Answers

Shweta_AgarwalShweta_Agarwal
Hi Shahid,

In you class in method parameter you are using List<Opportunity> and you have written trigger on Account object So it will pass List<account> in afterupdate method.

public static void afterUpdate (List<Opportunity> newList){}

So if want use same class in your account trigger then create a method with list<Account>

public static void afterUpdate (List<Account> newList){}

Hope this will help you

Thanks
Shweta
This was selected as the best answer
force shahidforce shahid
Hi Shweta,

Thanks for your information. Now Its working.

Thanks,
Shahid.