5

I am aware of the flow of execution in salesforce(validation, autoresponse, assignment...etc). I also have knowledge of the order of execution of a visualforce page. I have 2 questions on execution:

  • Say, if there are 5 BEFORE INSERT triggers on Account object, which one will trigger first?
  • Similarly, if I have 5 workflows on any object, what is the order of execution?

Or is it that order can be set only though code? I hope my question is not too wide.

Thanks in advance.

7

As @Bachovski pointed out, there is no way to control the execution order. In the case of your 5 account triggers, you would likely want to combine all 5 triggers into a single trigger that calls a utility class with 5 methods. Each method contains the logic of your old triggers. This way, you do have control over the order of execution.

Utility Class

public class AccountTriggerHelper {

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

Your new trigger

trigger AccountTrigger on Account (before insert){

     AccountTriggerHelper.myFirstMethod(trigger.new);
     AccountTriggerHelper.mySecondMethod(trigger.new);
     AccountTriggerHelper.myThirdMethod(trigger.new);
     AccountTriggerHelper.myFourthMethod(trigger.new);
     AccountTriggerHelper.myFifthMethod(trigger.new);
}

Then if you wanted to change the order of execution, you can just change a few lines around like below.

trigger AccountTrigger on Account (before insert){

         AccountTriggerHelper.myThirdMethod(trigger.new);
         AccountTriggerHelper.myFifthMethod(trigger.new);
         AccountTriggerHelper.mySecondMethod(trigger.new);
         AccountTriggerHelper.myFirstMethod(trigger.new);
         AccountTriggerHelper.myFourthMethod(trigger.new);

    }

Badda bing...you now can control the order of execution. It's also much cleaner with only 1 trigger and 1 utility class to maintain, as opposed to maintaining multiple triggers.

7

It's described nicely in the documentation:

The order of execution isn’t guaranteed when having multiple triggers for the same object due to the same event. For example, if you have two before insert triggers for Case, and a new Case record is inserted that fires the two triggers, the order in which these triggers fire isn’t guaranteed.

So there is no way to determine which trigger will fire first, it might be a completely different order every time.

Best practice is to have only 1 trigger per object and have the code separated in a handler class to keep the trigger code nice and clean.

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.