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?
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
Here is cheat-sheet of Order of execution. Salesforce performs the following events in below 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, Maximum field length.
- Executes flows that make before-save update. (New Change in Winter 20)
- Executes all before triggers.
- Runs most Custom validation.
- 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, updates the record again.
- 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.
- 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.
What is part of the after commit logic?
- All email sends
- Asynchronous Apex: @future methods
- Async Sharing Rule processing (for >25,000 records)
- Outbound Messages placed on queue
- Calculate Index, such as Search Index
- Render File Previews
- 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.
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.
Don’t forget to check below recording to learn about additional Considerations with some good example.
5,537 total views, 9 views today