Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks. Great answer :) –  MnZ Jul 30 at 15:06
    
No prob. Glad to help :) –  sfdc_ninja Jul 30 at 17:59

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.

share|improve this answer
    
Thanks for the info @bachovski –  MnZ Jul 28 at 11:46
1  
@MnZ here's a good salesforce developer blog by kevin on apex trigger patterns/frameworks : developer.salesforce.com/page/… –  Vamsi Krishna Jul 28 at 11:55
    
Thnks for the link krishna –  MnZ Jul 31 at 13:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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