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
Ashritha ReddyAshritha Reddy 

can any one explain the scenarios for recursive triggers?

can any one explain the scenarios for recursive triggers? with examples?
JyothsnaJyothsna (Salesforce Developers) 
Hi Ashritha,

Recursion occurs when the same code is executed again and again. It can lead to an infinite loop and which can result in governor limit sometime. Sometime it can also result in unexpected output.

It is very common to have recursion in the trigger which can result in unexpected output or some error. So we should write code in such a way that it does not result to recursion. But sometime we are left with no choice.

For example, we may come across a situation where in a trigger we update a field which in result invoke a workflow. The workflow contains one field update on the same object. So trigger will be executed two times. It can lead us to unexpected output.

Another example is our trigger fires on after update, and it updates some related object, and there is one more trigger on a related object which updates child object. So it can result from too infinite loop.

To avoid the situation of the 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 to make the variable false.

As an example let says you have the following trigger on contact:
trigger recursiveTrigger on Contact (after update) {
Id recordId ;
 for(contact con : trigger.new){
     recordId = con.id; 
 }
 Contact conRec =[select id,name,email from contact where id !=: recordId limit 1];

 conRec.email = 'testemail@kmail.com';
 update conRec;
 }
}

As you can see the trigger is getting called on after update and as the trigger has a DML update statement on the same object, the same trigger will be called again leading to recursion.

To avoid this, just create one global class with a static global boolean variable as below.
global Class recusrssionPreventController{
Global static boolean flag = true;
 Public recusrssionPreventController(){
 }
}

And set this boolean variable in your trigger and then set this boolean variable to false as done in the trigger below.

Trigger:
 
trigger recursiveTrigger on Contact (after update) {
Id recordId ;
if(recusrssivePreventController.flag == true){
   recusrssivePreventController.flag = false;
 for(contact con : trigger.new){
     recordId = con.id; 
 }
 Contact conRec =[select id,name,email from contact where id !=: recordId limit 1];

 conRec.email = 'testemail@kmail.com';
 update conRec;
 }
}


Hope this helps you!
Best Regards,
Jyothsna
Amit Chaudhary 8Amit Chaudhary 8
Problem :- 
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. 
 
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
    }
}
Let us know if this will help you

Thanks
Amit Chaudhary

 
PRASHANT BHARTIPRASHANT BHARTI
Hi Amit,

This is the case when there is one trigger. what if there are 4 triggers and any trigger can call any of the three rigger. So in this scenario how can we get rid of recursion .

Thanks & Regards,
Prashant
{tushar-sharma}{tushar-sharma}
Hi,
We have multiple ways to avoid recursive triggers. They all depend on your use case, While one solution may work but will not work for others. Check the below links, It has multiple ways to avoid recursive trigger: https://newstechnologystuff.com/2020/05/28/avoid-recursive-trigger-in-salesforce/