Activity Forums Salesforce® Discussions What is recursion problem in Salesforce Trigger? How to troubleshoot it?

  • Avnish Yadav

    Member
    July 5, 2018 at 5:31 am

    Hello Chanchal,

    Recursion in trigger means we are calling same method infinite times, this could happen when we have event 'update' in Account(let say) and in last we using DML query which Update in Account, therefore trigger undergoes to recursion.

    Avoiding a recrusion problem in trigger is easy. There are two methods we can use it to avoid recursion:-

    1. Create an Apex class with 'static' variable of boolean type and changes its value once update is done.
    2. Create an Apex class with 'static' variable of 'Set' type which store unique IDs of update records enter and check in trigger that if IDs exist in Set then don't do updates.

    Thanks.

  • madhulika shah

    Member
    September 10, 2018 at 1:09 pm

    Hi Chanchal,

    A recursive trigger is one that is called over and over, if not controlled will result in this error:

    maximum trigger depth exceeded

    Avoiding Recursive Triggers:

    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 transaction 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. Static variables defined in a trigger don’t retain their values between different trigger contexts within the same transaction, for example, between before insert and after insert invocations. Define the static variables in a class instead so that the trigger can access these class member variables and check their static values. With this approach in place the recursion stops once the trigger is called a second time, as it does not attempt to insert a third record. The result is for every one record a second additional record is created and the recursion is avoided.

    thanks.

  • Parul

    Member
    September 21, 2018 at 11:40 am

    Avoiding Recursive Triggers:

    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 = '[email protected]';
    update conRec;
    }
    }

    thanks.

  • Parul

    Member
    September 21, 2018 at 11:41 am

    Solution for this kind of scenario

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

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

    Trigger:

  • shariq

    Member
    September 22, 2018 at 3:10 am

    Hi,

    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.

    Hope this helps.

    • This reply was modified 6 years, 2 months ago by  shariq.
  • shariq

    Member
    September 22, 2018 at 3:12 am

    Hi,

    Try this -

    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 Set

    Check if the record exists in the set, if so do not execute the trigger, else execute the trigger.

    NOTE: Before deploying into production, we recommend testing in sandbox.

    This is a sample code written for Account object Class code
    public class checkRecursive {
         public static Set<Id> SetOfIDs = new Set<Id>();
    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs