Salesforce has a bunch of rules that can be defined on objects and fields. For example you can define validation rules, workflow rules, assignment rules, escalation rules, auto-response rules, triggers etc.
As an administrator and developer it is important for you to understand the order in which these rules and triggers are executed. Here is the gist of sequence in which Salesforce will evaluate different rules (For detailed explanation, please click here).
(By the way if you are preparing for Advanced Developer Certification i.e. DEV-501, there are a couple of questions on the order of execution)
- System Validation rule (required field, field format) (SV)
- Before Triggers are executed (BT)
- Custom Validation rules are checked (CV)
- After Triggers are executed (AT)
- Assignment Rules are executed (AR)
- Auto-Response Rules are executed (ARR)
- Workflow Rules are executed (WR)
- Before and after triggers are executed one more time if the workflow rule updates a field (BT & AT)
- Escalation Rules are executed (ER)
- Parent Rollup Summary Formula or Cross Object Formula fields are updated in the respective objects. (RSF, COF) (These parent records also goes through the entire execution order)
- Criteria Based Sharing rules are evaluated (CBS)
- Any Post-Commit Logic is executed (PCL) (like sending an email)
To remember this sequence you can memorise the following.
SV -> BT -> CV -> AT -> AR -> ARR -> WR (BT, AT) -> ER -> RSFCOF -> CBS -> PCL
Being aware of this sequence will help you configure your apps correctly and troubleshoot.
For example say there is a Lead assignment rule defined in Salesforce that assigns all leads to user “Ben Stuart”. Then there is a workflow rule with field update action defined that sets the owner of lead to user “Melissa Lee”. Now when a new Lead is created, the owner of the lead will appear as “Melissa Lee” as Salesforce will first run the lead assignment rule and set the owner to “Ben Stuart” and then it will run the workflow rule and set the owner to “Melissa Lee”. So the final outcome is that the lead owner is “Melissa Lee”
If you are 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 -> Monitor -> Logs -> 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 (click on image to enlarge).