anusha(salesforce developer)

Thursday, 16 June 2016

How to avoid recursive triggers?

Recursive Triggers

First of all you have to make sure that your trigger is executed just one time. For doing this, you have to make one class and add static boolean variable with default value true in it.

In the trigger, before executing your code keep  a check that the variable is true or not.

Once you check make the variable false.

Following is the code for that.


Class Code :

public Class checkRecursive{
      private static boolean check = true;
      
      public static boolean checkOneTime(){
               if(check){
                   check = false;
                   return true;
               }
               else{
                   return check;
               }
      }

}

Trigger Code :

trigger testTrigger on sObject(after update){
  
  if(checkRecursive.checkOneTime()){

    // Write Trigger Code/logic here     

  }


}

2 comments: