0

Trigger

trigger AccountTrigger on Account (after insert,after update,before insert, before update) {

    AccountTriggerHandler handler = new AccountTriggerHandler();

    if(Trigger.isInsert && Trigger.isBefore) {
        handler.OnBeforeInsert(Trigger.new, Trigger.newMap);
    } else if(Trigger.isUpdate && Trigger.isBefore){
        if(Recursive_helper.firstRun) {
            handler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
            Recursive_helper.firstRun=false;
        }
    } else if(Trigger.isUpdate && Trigger.isAfter) {
        // if(Recursive_helper.firstRun){ //shall i include this again in this context,i already include in before update context,Please correct me
        handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap);
          //    Recursive_helper.firstRun=false;}
    }
}

Class

public class Recursive_helper {  
    public static boolean firstRun = true;
}

Hi Experts,

Please clarify the below points

Suppose i'm trying to update 10000 accounts using Data Loader as a batch size 200 so the above trigger will execute 50 times. I'm trying to modify the code with stoping recursive fire due to workflows, other triggers, etc.

  1. Shall I include static variable(Recursive_helper.firstRun) in both before and after trigger??
  2. The above trigger with Recursive handling able to execute 50times or it will exits after first batch finish?

The above trigger planning to deploy as a managed package.Let me know any of things need to take care in case of managed package.

1
  • Quick question... do you want to prevent your handler from getting executed more than once regardless of which records, or you don't want to get the same records processed twice? – Sergio Alcocer Nov 3 '17 at 6:59
0

To answer your first question I would recommend to set the flag at the end of after trigger and add the check if(Recursive_helper.firstRun) at the top, wrapping the entire code.

In that way, the execution would be like:

  1. If codition is true, so Before Trigger gets executed.
  2. Other steps as per Order of Execution are executed.
  3. After Trigger gets executed and at the end of it the recursive flag is marked true.
  4. Now as the entire trigger is wrapped inside the if condition, the trigger won't be executed second time.

So the code will be like this:

trigger AccountTrigger on Account (after insert,after update,before insert, before update) {

AccountTriggerHandler handler = new AccountTriggerHandler();
if(Recursive_helper.firstRun) {
  if(Trigger.isInsert && Trigger.isBefore) {
    handler.OnBeforeInsert(Trigger.new, Trigger.newMap);
  } else if(Trigger.isUpdate && Trigger.isBefore){
    //if(Recursive_helper.firstRun) { //Commented this condition
        handler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
       // Recursive_helper.firstRun=false; //Comment this out.
    //}
  } else if(Trigger.isUpdate && Trigger.isAfter) {
    // if(Recursive_helper.firstRun){ //shall i include this again in this context,i already include in before update context,Please correct me
    handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap);
          Recursive_helper.firstRun=false; //Set the flag to false here.
  }
 }
}

Moving to send question:

The state of static variable stays as it is, in the entire transaction of the batch. Bulk data load, even if it is executed in batches, the governor limits and the static variable would be reset for each batch(I tried this myself)

1
  • He did say that he is going to execute the update with a batch-size of 200 ;) – Sergio Alcocer Nov 3 '17 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.