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
anitha sfdcanitha sfdc 

hi anybody have the realtime scenario of recursive trigger and how to avoid that, i want clear clarification on how it works please.

Lokesh KumarLokesh Kumar
HI Anitha,

Please have a look in the below articles for the same.

http://www.oyecode.com/2012/11/apex-design-pattern-to-avoid-recursive.html

http://salesforce.stackexchange.com/questions/46790/how-to-avoid-recursive-trigger-other-than-the-classic-class-w-static-variable

Thanks !
LBKLBK
I couldn't really think of a real-life use case for recursive trigger.

There are quite a few articles on Recursive triggers.

Here are couple of links.
https://help.salesforce.com/articleView?id=000199485&language=en_US&type=1

https://developer.salesforce.com/forums/?id=906F0000000Qtw4IAC

Hope this helps.
Arpit GargArpit Garg

Hi anitha,

Avoid recursive trigger in salesforce using static variable
Recursion occurs when same code is executed again and again. It can lead to infinite loop and which can result to governor limit sometime. Sometime it can also result in unexpected output.
It is very common to have recursion in trigger which can result to 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. Workflow contains one field update on 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 related object which updates child object. So it can result too infinite loop.
To avoid these kind of situation we can use public class static variable.
In RecursiveTriggerHandler class, we have a static variable which is set to true by default.

public class RecursiveTriggerHandler{
     public static Boolean isFirstTime = true;
}

In following trigger, we are checking if static variable is true only then trigger runs. Also we are setting static variable to false when trigger runs for first time. So if trigger will try to run second time in same request then it will not run.


trigger SampleTrigger on Contact (after update){
 
    Set<String> accIdSet = new Set<String>();
     
    if(RecursiveTriggerHandler.isFirstTime){
        RecursiveTriggerHandler.isFirstTime = false;
         
        for(Contact conObj : Trigger.New){
            if(conObj.name != 'SFDC'){
                accIdSet.add(conObj.accountId);
            }
        }
         
        // Use accIdSet in some way
    }
}

 

If you want more clear please visit below link:

https://help.salesforce.com/articleView?id=000133752&type=1

Thanks,
Arpit garg

 

Arpit GargArpit Garg
If your doubt clear..please choose my best. answer.

Thanku 
Ajinkya DhasAjinkya Dhas
HI ANITHA,

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 Scenario (https://salessforcehacks.blogspot.com/2019/12/salesforce-recursive-triggers-fully.html

Let us know if you require furhter information