How to avoid Recursive Trigger in Salesforce?

Sample Trigger:

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

Sample Classes:

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.

Cheers!!!

No comments:

Post a Comment