Master Order of Execution in Salesforce : Complete Guide
Nullam vel lectus vel velit pellentesque dignissim nec id magna. Cras molestie ornare quam at semper. Proin a ipsum ex. Curabitur eu venenatis
May 28, 2024
933 Views
If you are a Salesforce developer or administrator, having detailed insights about the order of execution in Salesforce is essential. Not understanding the sequence of events that occur on servers once a record is “saved” (inserted, updated, or upserted) can lead to undesired data modifications and flawed logic execution, ultimately resulting in inaccurate results.
In this blog, we will delve into the Salesforce Order of Execution, its steps, and why it matters for developers and admins.
Image Link – https://architect.salesforce.com/1/asset/immutable/s/e6cf2ac/assets/images/Salesforce-Order-Of-Execution-Diagram.png
Please note
that the diagram is specific to the API version specified. Refer to the Apex Developer Guide for the current order of execution.
Well, we have a couple of JavaScript validations running in the browser even before Salesforce fires server-side events.
What is Order of Execution in Salesforce?
Salesforce follows a specific set of steps when saving a record, referred to as the Salesforce order of execution. Think of it as a recipe that ensures data integrity and smooth application functionality. Understanding this sequence is critical for avoiding errors and building robust solutions.
Key Steps of Order of Execution in Salesforce
Here’s a detailed breakdown of the steps involved in Salesforce order of execution:
- Retrieve Record
- Retrieves the existing record from the database or sets up a new record for an upsert operation.
- Update Record
- Updates the record with new field values from the request, replacing the old values.
- Perform System Validation Checks (for standard UI edit page requests):
- Compliance with layout-specific rules.
- Ensures required field values are present.
- Validates field formats and maximum length.
- Custom Validation Rules
- Applies validation rules for User objects and other record types.
- Run Pre-Save Flows
- Executes record-triggered flows that run before the record is saved.
- Execute Before Triggers
- Executes all applicable before-save triggers.
- Perform Additional Validations
- Rechecks required fields and custom validation rules.
- Execute Duplicate Rules
- If a duplicate is detected and blocked, no further steps are taken.
- Temporarily Save Record
- Data is temporarily stored in the database, but changes are not yet committed.
- Execute After Triggers
- Executes all applicable after-save triggers.
- Execute Workflow Rules
- Updates the record again if any workflow field updates are triggered, followed by revalidations.
- Run Post-Save Automations
- Executes record-triggered flows, Process Builder processes, and other automation.
- Commit Changes to Database
- Finalizes all DML operations.
- Execute Post-Commit Logic
- Triggers actions such as sending emails, enqueuing asynchronous jobs, and executing entitlement rules.
Pro Tips for Developers and Admins
- Understand Trigger Behavior:
Trigger.old
does not reflect field updates made by workflow rules after a record update.
- Handle Static Variables Cautiously:
- Triggers can execute multiple times within a transaction. Static variables are not reset between executions.
- Account for Roll-Up Summary Fields:
- Roll-up summary field calculations propagate changes up the hierarchy (e.g., parent and grandparent records).
Practical Use Case:
Let’s say you’re building an automation to update an opportunity’s stage based on certain criteria. Understanding the order of execution ensures your before-save trigger executes correctly before workflow field updates modify the same record.
Key Points to Remember about Order of Execution in Salesforce
- Trigger. old does not represent field updates that are made by workflow rules after the record is updated. It will contain field values before the initial update.
- Once the DML operation is tried with partial success, triggers can be executed multiple times within the transaction. Between these trigger executions, static variables on classes accessed by those triggers are not reset.
- Execution order is not guaranteed for multiple triggers on the same object for the same event type.
- Inserting a non-private contact and then associating it with various accounts in multiple orders can be found on AccountContactRelation.
- For details on using before triggers to set Stage and Forecast Category in opportunities, one should refer to the Opportunity section.
- In API version 53.0 and prior versions, after-save record-triggered flows are executed after entitlement processing is done.
Also Read
Don’t forget to checkout: Salesforce Configuration vs. Customization: Which One Is Better for Your Business?.
Final thoughts
In short, Order of Execution is fundamental for any developer or admin working in the Salesforce space. Understanding this flow guarantees that you develop applications and automation that work correctly.
Developers can build solid and reliable solutions by understanding how Salesforce processes records, including (but not limited to) validation rules, triggers, workflows, etc. Moreover, it’s also essential to be conscious of the possibility of future changes in each new release Because this is ultimately about ensuring that logic and data integrity remain reliable over time.
As a result, thorough knowledge of the Order of Execution strengthens existing operations and prepares you for flexibility in upcoming features. It’s evident that staying informed through official Salesforce documentation, such as the Apex Developer Guide, is imperative for continued success in the dynamic landscape of Salesforce development.
That’s why it is clear that in the rapidly evolving world of Salesforce development, you must remain current through well-documented resources like official Salesforce documentation / Apex Developer Guide.
References to explore more about this
Triggers and Order of Execution – https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
Apex developer Guide – https://developer.salesforce.com/docs/atlas.en-us.250.0.apexcode.meta/apexcode/apex_dev_guide.htm
To control the order of execution of Salesforce Flow automations, use record-triggered flows. See Manage Record-Triggered Flows – https://help.salesforce.com/s/articleView?id=sf.flow_trigger_explorer.htm&language=en_US
- Share this article
Contributor of the month
Mithun Naik
Software Engineer | Salesforce | 3X Certified Developer | Salesforce Blog Writer
Categories
Most Viewed Posts
Boost Your Brand's Visibility
Want to promote your products/services in front of more customers?
Explore More Blogs
Default title
This will close in 0 seconds