Consolidate trigger methods or not?

I have a general best practices question about triggers. I have created a handful of triggers that perform different actions but are called by the same event, such as after account update/insert/delete.

Is it best to keep those triggers separate and name them accordingly (i.e. UpdateMyCustomFieldOnAccountUpdate)? Or, is it best to have a general ‘AccountTrigger’ trigger that contains all the functions that should be performed on Account update/insert/delete? And by doing so, do you avoid recursive trigger execution?

Thank you in advance for any help.

Answer

It is general best practice to consolidate. The main advantage here is control of execution. When you have 2 triggers on the account object, there is no way to know or control which fires first. If however, you move those triggers into a utility class, called by a single account trigger, you now can control the order in which they are fired.

2 Trigger approach

trigger AccountTrigger1 on Account (before insert){
     //your logic
}

trigger AccountTrigger2 on Account (before insert){
     //your otherlogic
}

When you insert an account, sometimes trigger1 will fire first, and sometimes trigger2 will fire first, there is no way to predict or control this.

Consolidated approach

trigger AccountTrigger1 on Account (before insert){

     AccountTriggerHelper helper = new AccountTriggerHelper();

     helper.myFirstMethod(trigger.new);
     helper.mySecondMethod(trigger.new);
}

public class AccountTriggerHelper {

     public void myFirstMethod(list<Account> accs) {
          //your logic
     }
     public void mySecondMethod(list<Account> accs) {
          //your logic
     }
}

So now you can see that you have control over the order of execution of your trigger logic.

You could just as easily change the order of execution by changing the order of method calls in your trigger like below

trigger AccountTrigger1 on Account (before insert){

     AccountTriggerHelper helper = new AccountTriggerHelper();

     helper.mySecondMethod(trigger.new);   //SWITCHED ORDER
     helper.myFirstMethod(trigger.new);
}

EDIT

To control recursion, you would still want to utilize static variables. I generally do this with another class. Something like this

public class TriggerContextUtility {

    private static boolean firstRun = true;

    public static boolean isFirstRun() {
        return firstRun;
    }

    public static void setFirstRunFalse(){
        firstRun = false;
    }
}

Then you would use this in your trigger like this

trigger AccountTrigger1 on Account (before insert){

     AccountTriggerHelper helper = new AccountTriggerHelper();
     If(TriggerContextUtility.isFirstRun()) {
          TriggerContextUtility.setFirstRunFalse();
          helper.myFirstMethod(trigger.new);
          helper.mySecondMethod(trigger.new);
     }
}

Attribution
Source : Link , Question Author : Adam H. , Answer Author : Chris Duncombe

Leave a Comment