Order of execution in Salesforce : Hema
by: Hema
blow post content copied from Apex Hours
click here to view original post
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
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)
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.
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.
45 total views, 45 views today
The post Order of execution in Salesforce appeared first on Apex Hours.
November 03, 2020 at 01:11AM
Click here for more details...
=============================
The original post is available in Apex Hours by Hema
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment