Become a Salesforce Order of Execution Hero

Share this article...

If you’re a Salesforce professional, the order of execution in which events happen once you save a record in Salesforce is vital to understand. It will help you strategise solutions, as well as debug issues.

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 automation, and the events that happen when you save a record with an insert, update, or upsert statement.

Check out this ApexHours session on “Become an Order of Execution hero” to understand how to design robust apps, optimized for scale, and how to control typical recursion and remedy problems.

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:

  1. Loads Initial record.
  2. 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.
  3. Executes flows that make a “before-save” update (new change in Winter 20).
  4. Executes all before triggers.
  5. Runs most custom validation rules.
  6. Executes duplicate rules.
  7. Saves the record to the database, but doesn’t commit yet.
  8. Executes all after triggers.
  9. Assignment rules.
  10. Executes auto-response rules.
  11. Executes workflow rules.
  12. If there are workflow field updates, then it updates the record again.
    1. If the Workflow field updates introduced new duplicate field values, executes duplicate rules again.
    2. 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.
    3. Custom validation rules are not run again.
  13. Executes processes and flow.
  14. Escalation rules.
  15. Executes entitlement rules.
  16. Executes record-triggered flows that are configured to run after the record is saved.
  17. 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.
  18. 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.
  19. Executes Criteria Based Sharing evaluation.
  20. Commits all DML operations to the database.
  21. Executes all after-commit logic, such as sending email.

Additional Considerations

As you’ve seen, when it comes to Salesforce 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.

6 thoughts on “Become a Salesforce Order of Execution Hero

  1. No. Process builder also gets back to the before trigger, so we can have even three before triggers.

  2. This is great! I wish this diagram highlighted the “Save” and “Commit” steps in another color for reference, like a landmark.

Add Comment