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 ‘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 ‘Debug Logs’ feature in Salesforce will come to your rescue.
Let’s say that we have configured the following on Lead object
- There is a validation rule on the lead object that states that if the lead rating is hot, the email cannot be blank
- Then we have configured duplicate rules in Salesforce to ensure that the lead being created does not already exist
- Then there is a Lead assignment rule defined in Salesforce that assigns all “Hot” leads to the user “Nick Admas”.
- Then there is a workflow rule with field update action defined that sets the Industry picklist field of Lead to “Technology” if City is San Francisco.
- 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)
Reference & Useful URLs
- 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
- 20 Different Monitoring & Auditing Tools in Salesforce – Blog Post (20 mins) – https://www.asagarwal.com/monitoring-and-auditing-tools-in-salesforce/
Blog Post Change Log
- August 2015 – First Published
- August 2019 – Updated to include duplicate rules, process builders & flows
- November 2020 – Updated as per Winter ’21 release to include record triggered flows