Activity Forums Salesforce® Discussions How to stop recursive trigger in Salesforce?

  • Achintya

    Member
    September 9, 2019 at 7:42 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 to make the variable false.

    Example:

    Apex Code:

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

    Trigger Code:

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

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos