Maximum Trigger Depth Exceeded Error Salesforce

Maximum Trigger Depth Exceeded Error Salesforce

Maximum Trigger Depth Exceeded Error Salesforce occurs mainly due to recursion in the trigger. Recursion can occur because of many reasons. 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. Sometimes it can also result in unexpected output.

In the following trigger, we are checking if contact last name is not equal to SFDC, then we are preparing set of account id. Now let’s assume that we are updating account record in this trigger and there is one more trigger which updates contact record when accounts are updated

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

We will get an error in the above example if there is recursion Maximum Trigger Depth Exceeded Error.

Solution to resolve Maximum Trigger Depth Exceeded Error Salesforce

To avoid these kind of situation we can use public class static variable. We can solve this issue, you can set a condition on trigger so it will not be called recursively.

In RecursiveTriggerHandler class, we have a static variable that is set to true by default.

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

In the following trigger, we are checking if the static variable is true only then trigger runs. Also, we are setting a static variable to false when trigger runs for the first time. So if the trigger will try to run second time in the 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 to update account
    }
}

For more details refer to Avoid recursive trigger in salesforce using static variable

Permanent link to this article: https://www.sfdcpoint.com/salesforce/maximum-trigger-depth-exceeded-error-salesforce/

1 comment

    • Aradhika Chawla on January 26, 2021 at 10:46 pm
    • Reply

    // This below code worked as well to avoid maximum trigger with error//

    List beforelistOppw = [select id , name, accountId, importantNotes__c from Opportunity where accountId in :listOppAccountnotes.keySet()];
    System.debug(‘beforelistOppw:=’+ beforelistOppw);
    Set r = new Set();

    for (Opportunity o : beforelistOppw){
    // System.debug(‘listOppAccountnotes.get(o.AccountId):=’+listOppAccountnotes.get(o.AccountId));
    if (o.importantNotes__c!=listOppAccountnotes.get(o.AccountId)){
    o.importantNotes__c = listOppAccountnotes.get(o.AccountId);
    r.add(o);
    }
    }
    List lr = new List(r);
    update lr;
    List afterlistOppw = [select id , name, accountId, importantNotes__c from Opportunity where accountId in :listOppAccountnotes.keySet()];
    System.debug(‘afterlistOppw:=’+ afterlistOppw);

Leave a Reply

Your email address will not be published.