Order of execution in Salesforce

Order of execution in Salesforce

In this post we will review the order of execution in Salesforce when we save a record with an insert, update, or upsert statement. This is important to understand if you are an Salesforce Admin who works on automating processes or if you are a developer that writes trigger code. Both worlds will converge in the end and affect each other, so you have to bear in mind how Salesforce executes things internally.

What is Order of Execution in Salesforce?

A set of rules that describe the path a record takes through all automations and the events that happen from SAVE to COMMIT. Before Salesforce executes these events on the server, the browser runs JavaScript validation if the record contains any dependent picklist fields. The validation limits each dependent picklist field to its available values. No other validation occurs on the client-side.

Check our old session on “Become an Order of Execution hero” to walk away with an understanding of how to design robust apps optimized for scale and how to control recursion and remedy typical problems.

Lots of person struggling to remember and visualize the order of execution which starts with a DML operation and there are involvements of triggers, different types of out-of-box automations like process builder, workflows, flows and different rules. I hope below diagram could be easy to understand and remember taking reference.

Order of Execution
Order of Execution

Order of Execution

Here is cheat-sheet of Order of execution. Salesforce performs the following events in below 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, Maximum field length.
  3. Executes  flows that make before-save update. (New Change in Winter 20)
  4. Executes all before triggers
  5. Runs most Custom validation.
  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, updates the record again.
  13. Due to Workflow field updates introduced new duplicate field values, executes duplicate rules again. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules are not run again.
  14. Executes processes and flow. 
  15. Escalation rules
  16. Executes entitlement rules
  17. Executes record-triggered flows that are configured to run after the record is saved.
  18. 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. 
  19. 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. 
  20. Executes Criteria Based Sharing evaluation
  21. Commits all DML operations to the database
  22. Executes all after-commit logic, such as sending email.

What is part of the after commit logic?

  1. All email sends
  2. Asynchronous Apex: @future methods
  3. Async Sharing Rule processing (for >25,000 records)
  4. Outbound Messages placed on queue
  5. Calculate Index, such as Search Index
  6. Render File Previews
  7. Publication of Platform Events (if configured)

Operations That Don’t Invoke Triggers

  • Cascading delete operations. Records that did not initiate a delete don’t cause trigger evaluation.
  • Cascading updates of child records that are reparented as a result of a merge operation
  • Mass campaign status changes, Mass division transfers, Mass address updates, Mass approval request transfers, Mass email actions
  • Modifying custom field data types
  • Renaming or replacing picklists
  • Managing price books
  • Changing a user’s default division with the transfer division option checked
  • Update account triggers don’t fire before or after a business account record type is changed to person account (or a person account record type is changed to business account.)
  • Update triggers don’t fire on FeedItem when the LikeCount counter increases.

Additional Considerations

The order of execution isn’t guaranteed when having multiple triggers for the same object due to the same event. For example, if you have two before insert triggers for Case, and a new Case record is inserted that fires the two triggers, the order in which these triggers fire isn’t guaranteed. Please implement Trigger framework for same.

Recording

Don’t forget to check below recording to learn about additional Considerations with some good example.

5,537 total views, 9 views today

Leave a Comment