+ Start a Discussion
sudhakar reddy 13sudhakar reddy 13 

what is Recursive trigger and give one example.

Abhishek BansalAbhishek Bansal
Hi,

If a trigger is callled again and again than it is called recursive trigger.

For Eg:
trigger on Contact (before update){
    update anyContactList;
}

In the above example we are updating contact list in before update trigger so it will again call the trigger and our trigger will be called recursively till we stop or block it with the help of a static variable.

You can find more details for avoiding your trigger to be called recursively in below link :
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)

Regards,
Abhishek
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 :-

http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html
 
Amresh Kumar 66Amresh Kumar 66
Recursive Trigger: If same trigger runs for infinite times then it is called as recursive trigger.

Cause: Recursive trigger comes into picture whenever there is an "update" event on trigger.

Solution: To avoid recursive trigger we must use static variable in handler class with value assigned to "TRUE".
Inside trigger first we check the variable is TRUE and then make it FALSE before updation code of trigger.
Ajinkya DhasAjinkya Dhas
HI SUDHAKAR,

Recursive Trigger
When you want to write a trigger that creates a new record as part of its processing logic. However, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. You don't know how to stop that recursion.

Using a static variable in an Apex class to avoid an infinite loop. A static variable is local to the context of a web request (or test method during a call to runTest()), so all triggers that fire as a result of a user's action which has access to it.

Let's understand some scenarios...

Scenario :
Suppose there is a scenario where one trigger perform an update operation, which results in invocation of the second trigger and the update operation in second triggers acts as triggering criteria for triggers one.

Apex Class :
============================================
public class utility
{
 public static boolean isFutureUpdate;
}

============================================

Apex Class 2 :
============================================
public class FutureMethods
{
  @future
 public static void processLargeAccounts(set<Id> accIds)
 {
  List<Account> accToUpdate = new List<Account>();

/* isFutureUpdate is set to turn to avoid recurssion */
  utility.isFutureUpdate = true;
  update accToUpdate;
 }
}

============================================

Trigger :
============================================
trigger updateSomething on Account(after insert, after update)
{

 /*This trigger performs its logic when the call is not from @future*/
 if(utility.isFutureUpdate != true)
    {
        set<Id> idsToProcess = new Set<Id>();
        for(Account acc : trigger.new)
        {
            if(acc.NumberofEmployers > 500)
            {
                 idsToprocess.add(acc.Id);                    
            }
        }
   
    /* Sending Ids to @future method for processing*/
        FutureMethods.processLargeAccounts(idsToprocess);
    }
  }

============================================

In the above scenario, it will first check utility.isFutureUpdate != true from utility class. If it is false that means, a call is not from the future method then it will perform its logic.

Then it will send its Ids to the FutureMethods class to process further.

For more scenarios and error problems please visit :
https://www.salesforcekid.com/2019/07/recursive-triggers-in-salesforce.html

I hope this will help you to solve your problem.
HAPPY LEARNING ☁️⚡️

AJINKYA DHAS ☁️⚡️
Salesforcekid  (http://www.salesforcekid.com)
raj_sfdccraj_sfdcc
Hi,
In general Recursion is executing the same task repeatedly. The Recursive trigger occurs when same trigger called it self multiple times which results in infinite loop. Which would throw an error without commiting the changes to database.

Please find the below link with full explanation with example :
Recursive Trigger (https://salessforcehacks.blogspot.com/2019/12/salesforce-recursive-triggers-fully.html)
{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/
Many Developers face recursive trigger or recursive update trigger. For example in 'after update' trigger, the Developer is performing update operation, and this leads to the recursive call. Or if we have WF field update sometimes they also contribute to recursive triggers.