Share Your Experience With Others

How to stop Recursive Trigger

Sometimes we face the problem of recursive trigger. The solution of this problem is:

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 Update the variable value false.

Apex Class with Static Variable
public class TriggerHandler
{
     public static Boolean isFirstTimeTriggerCalled = true;
}


Trigger Code
trigger AccountTrigger on Account (after update)
{
    Set<String> accIdSet = new Set<String>(); 
    if(TriggerHandler.isFirstTimeTriggerCalled)
    {
        TriggerHandler.isFirstTimeTriggerCalled = false;
         //Any Code Here
    }
}

2 responses to “How to stop Recursive Trigger”

  1. Ankit Avatar
    Ankit

    Hi Kamaal,

    Getting Recursive Trigger ,Please find below Code and let me know why getting this error :

    Triiger Code :

    trigger AccountTrigger on Account (after insert, after update, after delete, after unDelete) {

    if(Trigger.isAfter && Trigger.isInsert){
    System.debug(‘######## 1.Trigger is After Insert :’+trigger.new);
    if(AccountTriggerHelper.triggerOnOff){
    AccountTriggerHelper.triggerOnOff = false;
    AccountTriggerHelper.afterInsert(Trigger.new);

    }
    }

    if(Trigger.isAfter && Trigger.isUpdate){
    System.debug(‘######## 2.Trigger is After update :’+trigger.new);
    System.debug(‘######## 3.Trigger is After update :’+Trigger.oldMap);

    AccountTriggerHelper.afterUpdate(Trigger.new, Trigger.oldMap);
    }

    if(Trigger.isAfter && Trigger.isDelete){
    AccountTriggerHelper.afterDelete(Trigger.old);
    }

    if(Trigger.isAfter && Trigger.isUnDelete){
    AccountTriggerHelper.afterUnDelete(Trigger.new);
    }

    }

    Helper Code :

    public class AccountTriggerHelper {
    public static boolean triggerOnOff = true;
    public static void afterInsert(List newList){
    System.debug(‘##### 1.Trigger Helper After Insert : ‘+newList);
    handleRollup(newList, null);
    }

    public static void afterUpdate(List newList, Map oldMap){
    System.debug(‘##### 2.Trigger Helper After update : ‘+newList);
    System.debug(‘##### 3.Trigger Helper After update : ‘+oldMap);

    handleRollup(newList, oldMap);
    }

    public static void afterDelete(List oldList){
    handleRollup(oldList, null);
    }

    public static void afterUnDelete(List newList){
    handleRollup(newList, null);
    }
    private static void handleRollup(List newList, Map oldMap){
    System.debug(‘###### 4. Final Method call :’+ newList +’ #########’+oldMap);
    List acList = new List();
    Set accountIdSet = new Set();
    for(Account ac:newList){
    if(oldMap == null && ac.fax == null){
    accountIdSet.add(ac.id);
    }
    else if(oldMap != null){
    System.debug(‘###### 5. Final Method call :’+oldMap);
    accountIdSet.add(ac.id);
    }
    }

    for(Account a :[Select Id,Name,website,SLA__c,SLASerialNumber__c from Account where Id IN :accountIdSet ]){
    if(oldMap == null && a.website == null){
    a.Website = ‘www.google.com’;
    acList.add(a);
    }

    else if(oldMap != null){
    System.debug(‘###### 7. Final Method call :’+oldMap);
    if(a.SLA__c == null){
    if(a.SLA__c == ‘Gold’){
    a.SLASerialNumber__c = ’10’;
    }
    else if(a.SLA__c == ‘Silver’){
    a.SLASerialNumber__c = ’20’;
    }
    else{
    a.SLASerialNumber__c = ’30’;
    }
    acList.add(a);
    }
    }
    }
    upsert acList;
    }
    }

    Like

    1. Kamaal Ahmad Usmani Avatar
      Kamaal Ahmad Usmani

      Ok Ankit I will check and let you know

      Like

Leave a comment