Activity Forums Salesforce® Discussions What is recursive trigger in salesforce

  • Shubham

    Member
    July 17, 2017 at 12:00 pm

    Hi absar

    A recursive trigger is one that performs an action, such as an update or insert, which invokes itself owing to,  say something like an update it performs.

     

    eg in a before trigger, if you select some records and update them, the trigger will invoke itself.

     

    To avoid, static variable 'locks' are used. Illustrated in the salesforce doc

     

    "Static variables are only static within the scope of the request. They are not static across the server, or across the entire organization.

    Use static variables to store information that is shared within the confines of the class. All instances of the same class share a single copy of the static variables. For example, all triggers that are spawned by the same request can communicate with each other by viewing and updating static variables in a related class. A recursive trigger might use the value of a class variable to determine when to exit the recursion."

     

    http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm

  • Aman

    Member
    July 17, 2017 at 12:09 pm

    Hello Absar,

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

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos