function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CvrKCvrK 

How does static boolean variable helps in avoiding recursive triggers?

Hi,
I have seen lot of examples in the posts about avoiding recursive triggers,what i wonder why the boolean value is set to '"true" in helper class that handles the trigger events and why the boolean value is set to "false" in trigger.
can someone explain or post some sample code if possible eith comments so that it make sense to me what the boolean value is exactly doing the helper class and in the trigger.
pconpcon
If you take a look at this example [1] you can see how it can be used.  This will only run once because the static boolean stays "alive" for the entire transaction.

[1] https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

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
 
public class ContactTriggerHandler
{
     public static Boolean isFirstTime = true;
}
trigger ContactTriggers on Contact (after update)
{
    Set<String> accIdSet = new Set<String>(); 
    if(ContactTriggerHandler.isFirstTime)
    {
        ContactTriggerHandler.isFirstTime = false;
         for(Contact conObj : Trigger.New)
  {
            if(conObj.name != 'Test') 
     {
                accIdSet.add(conObj.accountId);
            }
         }
   // any code here
    }
}
Please let us know if this will help you

Thanks
Amit Chaudhary