Activity Forums Salesforce® Discussions How can we avoid recursive trigger execution in Salesforce?

  • Nikita

    Member
    August 29, 2019 at 10:27 am

    Hi Achintya,

    To avoid the recursion on a trigger, make sure your trigger is getting executed only one time. You may encounter the error : 'Maximum trigger depth exceeded', if recursion is not handled well.

  • Deepanshu Singh

    Member
    January 18, 2020 at 5:40 pm

    Recursive Trigger problem occurs usually on update Trigger, you have solve the problem by adding this code :

    public class CheckRecursive {
        private static boolean run = true;
        public static boolean runOnce() {
            if(run) {
                run = false;
                return true;
            }
            else {
                return run;   
            } 
        }
    }

    if((Trigger.isBefore || Trigger.isAfter) && Trigger.isUpdate ))

    {

    if(CheckRecursive.runOnce()) {

    // your further code here

    }

    }

  • Sumit

    Member
    January 29, 2020 at 11:48 am

    To avoid recursive triggers you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos