Order of Execution (Rules, Triggers etc.) in Salesforce & Debug Log

Salesforce has a bunch of rules that can be defined on objects and fields. For example, you can define validation rules, workflow rules, process builder, flows, assignment rules, escalation rules, auto-response rules, triggers, etc.

Whether you are an administrator, consultant, developer, or architect — it is important for you to understand the order in which these rules and triggers are executed.

The following picture depicts the order of execution visually. (For finer details, please refer to the Apex Developer Guide URL mentioned under the 'References & Useful URLs' section below).

Now, at times things will not work as you expect it to. Your system may behave like a drunk (or at least you would think so). In such cases, the 'Debug Logs' feature in Salesforce will come to your rescue.

Let's say that we have configured the following on the Lead object.

  1. There is a validation rule on the Lead object that states that if the lead rating is Hot, the email cannot be blank.

  2. Then we have configured duplicate rules in Salesforce to ensure that the lead being created does not already exist.

  3. Then there is a Lead assignment rule defined in Salesforce that assigns all "Hot" leads to the user "Nick Adams".

  4. Then there is a workflow rule with a field update action defined that sets the Industry picklist field of Lead to "Technology" if City is San Francisco.

  5. Next, there is a Process Builder with criteria: if Lead Status is "Site Visit" then a Site Visit record should be created automatically, mapping values from Lead.

If you are using a combination of different features in Salesforce and running into issues where the system is not behaving the way you expect it to, the best way to diagnose and troubleshoot will be to turn the debug on (Setup → Debug Logs), execute the transaction, and then check the debug log. Based on the example given above, here is what you will see in the debug log (hover on these images to enlarge).

Keep this sequence in mind while designing your solution and your app will behave properly.

(By the way, if you are preparing for Platform Developer Certification, there are a couple of questions on the order of execution.)

References & Useful URLs

  1. Triggers and Order of Execution — Apex Developer Guide (15 mins) — https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

  2. Blog Post #26 Different Monitoring & Auditing Tools in Salesforce