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
sudhakar reddy 13sudhakar reddy 13 

What is recursive trigger and how it can be overcome – with example

Muthuraj TMuthuraj T

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

 

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            
    }

}
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


Solution :-
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

Apex Class with Static Variable :-
public class ContactTriggerHandler
{
     public static Boolean isFirstTime = true;
}
Trigger Code :- 
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