+ Start a Discussion

How does static boolean variable helps in avoiding recursive triggers?

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.
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

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>(); 
        ContactTriggerHandler.isFirstTime = false;
         for(Contact conObj : Trigger.New)
            if(conObj.name != 'Test') 
   // any code here
Please let us know if this will help you

Amit Chaudhary