If you’re a Salesforce developer or administrator, knowing the order of execution is key to avoiding data errors, flawed logic, and slow performance.
When you save a record (insert, update, or upsert), Salesforce follows a structured sequence to keep data accurate.
This guide breaks down Order of execution in salesforce, why it matters, and how to optimize your automation strategies.
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 predefined sequence of operations when processing DML (Data Manipulation Language) statements. Think of it as a step-by-step recipe that ensures validation rules, triggers, and workflows function correctly.
Without a clear understanding of this execution flow, developers may overwrite field values, create recursive loops, or cause automation conflicts.
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: Why Order of Execution Matters
Imagine you’re building an automation to update an opportunity’s stage based on specific conditions. If you don’t account for the order of execution:
- A before-save trigger may execute before a workflow updates a field.
- This could result in incorrect logic execution or unexpected overwrites.
By understanding Salesforce’s execution flow, you can optimize your automation for better performance and reliability.
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