Friday 30 June 2017

Avoid Recursive Trigger Calls

Description:

Many developers face this issue because of a recursive trigger. For example,
 in an 'after update' trigger, a developer could be performing an update 
operation and this would lead to recursive call, and the error:
"maximum trigger depth exceeded"

Resolution:

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<string> variable .

In the trigger, before executing your code keep a check that the string contains
trigger or not.based on that it will execute only once.

Below same code we will add where ever recursion happen.

public with sharing class RecursiveCheck {
    public static Set<String> triggerMonitor = new Set<String>();    
}

trigger ContactTrigger on Contact (after insert) {
    if(trigger.isInsert && !RecursiveCheck.triggerMonitor.contains('NAV_CRM_ContactTrigger')){
        RecursiveCheck.triggerMonitor.add('NAV_CRM_ContactTrigger');
        ====your logic here====
    }  

}


where ever your doing DML in class just need to add below line, it will stop recursion. 

if(updContacts.size() > 0){
 RecursiveCheck.triggerMonitor.add('ContactTrigger'); //Here we need to add trigger name. 
 database.update(updContacts); 
 }



Recursive Triggers

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...