Activity › Forums › Salesforce® Discussions › How can we avoid recursive trigger execution in Salesforce?
-
How can we avoid recursive trigger execution in Salesforce?
Posted by Achintya on August 29, 2019 at 10:23 AMHow can we avoid recursive trigger execution in Salesforce?
Sumit kumar replied 6 years, 4 months ago 4 Members · 3 Replies -
3 Replies
-
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.
- [adinserter block='9']
-
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
}
}
-
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.