How to avoid Recursive Trigger in Salesforce?

How to avoid Recursive Trigger in Salesforce?

Maximum trigger depth exceeded Salesforce exception is thrown when the trigger recursion is not handled well in the code.

Sample Trigger:

trigger AccountTrigger on Account(after insert, after update){
 AccountHandlerController.perform(trigger.New);
}

Sample Classes:

Class for Trigger Handler: 

Class for Trigger Handler: 
public class AccountHandlerController{
        if(CommonUtility.check){
                CommonUtility.check = false;
         public static perform(List<Account> listAccount){
           for(Account acct : listAccount){
   ..................
   ..................
   ..................
          }
          update listAccount;
         }
        }
}

Class for storing boolean variable

public class CommonUtility{
        public static Boolean check = true;
}

If the Boolean variable is not used inside the class, it will cause infinite loop and we will face “maximum trigger depth exceeded exception” in Salesforce.

Note:

The boolean variable should be set to false first and not at the last.

Leave a Reply