Activity Forums Salesforce® Discussions How to avoid recursive trigger calls in Salesforce?

  • Suraj

    Member
    April 18, 2017 at 1:45 pm

    Hi Manpreet,

    In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, 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.

     

  • Avnish Yadav

    Member
    September 30, 2018 at 3:51 am

    Here is the code-

    Apex class:-

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

    Trigger -

    trigger updateTrigger on anyObject(after update) {
        if(checkRecursive.runOnce())
        {
            //write your code here
        }
    }

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos