In this post we will talk about how to become an Order of execution Hero!
What is Order of Execution in Salesforce
Order of execution is a set of rules that describe the path a record takes through all automations and the events that happen when you save a record with an insert, update, or upsert statement.
Many people struggle to remember the order of execution which starts with a DML (insert, update or delete). The diagram below provides a nice visual reference.
Order of Execution
When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in this order:
- Loads Initial record.
- If the request came from a standard UI edit page, Salesforce runs system validation to check the record for page layout specific rules, field definition, and maximum field length.
- Executes flows that make a “before-save” update (new change in Winter 20).
- Executes all before triggers.
- Runs most custom validation rules.
- Executes duplicate rules.
- Saves the record to the database, but doesn’t commit yet.
- Executes all after triggers.
- Assignment rules.
- Executes auto-response rules.
- Executes workflow rules.
- If there are workflow field updates, then it updates the record again.
- If the Workflow field updates introduced new duplicate field values, executes duplicate rules again.
- If the record was updated with workflow field updates, it fires before and after update triggers one more time (and only one more time) in addition to standard validation.
- Custom validation rules are not run again.
- Executes processes and flow.
- Escalation rules.
- Executes entitlement rules.
- Executes record-triggered flows that are configured to run after the record is saved.
- If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
- If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through save procedure.
- Executes Criteria Based Sharing evaluation.
- Commits all DML operations to the database.
- Executes all after-commit logic, such as sending email.
As you’ve seen, when it comes to order of execution, there’s a lot more to know than triggers run before workflows. Dig deeper, understand the logic flow and your apps will scale better and run more reliably. Check out this ApexHours session to learn about additional considerations with some good examples.