
When working with Salesforce, understanding the order of execution in Salesforce is crucial for building reliable, scalable, and efficient applications. Whether you’re developing triggers, workflows, or validation rules, knowing the sequence in which these events occur can help you avoid common pitfalls such as recursion, unexpected errors, or incomplete data updates.
In this article, we’ll break down the order of execution in Salesforce step-by-step, including what happens behind the scenes when a record is inserted, updated, or deleted.
What is Order of Execution in Salesforce?
Order of execution refers to the sequence in which Salesforce performs automated processes—like triggers, validation rules, workflows, and process builders—when a record is saved to the database.
This execution flow ensures data integrity, proper rule enforcement, and helps in debugging and testing scenarios.
Why Is It Important?
Understanding the order of execution in Salesforce allows you to:
-
Prevent recursive triggers
-
Avoid conflicts between automation tools
-
Write efficient Apex code
-
Ensure that business rules are consistently applied
Step-by-Step Order of Execution in Salesforce
Here’s the standard 21-step order of execution Salesforce follows when a DML operation (like insert/update) occurs:
1. Load the Original Record
-
If it’s an update, Salesforce loads the existing record from the database.
2. Load New Field Values
-
Fields are overwritten with new values from the incoming request.
3. System Validations
-
Required fields, field formats, foreign keys, etc., are validated.
4. Before Triggers
-
Executes all “before insert” or “before update” triggers.
5. Custom Validations
-
Executes any validation rules defined in Salesforce.
6. Duplicate Rules
-
Duplicate management runs and may block the save.
7. After Triggers
-
Executes “after insert” or “after update” triggers.
8. Assignment Rules
-
Runs assignment rules (mainly for Leads and Cases).
9. Auto-Response Rules
-
Sends automatic responses based on specific rules.
10. Workflow Rules
-
Evaluates and executes workflows:
-
Field updates
-
Tasks
-
Email alerts
-
Outbound messages
-
11. Workflow Field Updates
-
If field updates occur, before triggers and custom validations are re-executed.
12. Processes and Flows
-
Invokes Process Builder and auto-launched Flows.
13. Escalation Rules
-
For Cases, escalation rules are run.
14. Entitlement Rules
-
For support-related objects, entitlement rules apply.
15. Roll-Up Summary Fields
-
If any parent records are affected, roll-up summary fields recalculate.
16. Criteria-Based Sharing Rules
-
Applies sharing rules based on record criteria.
17. Post-Commit Logic (Asynchronous)
-
Enqueued jobs like:
-
Email Sends
-
Async Apex: Queueable, Future, Batch
-
Platform Events
-
18. Commit to Database
-
Final data is saved to the database.
19. Post-Commit Triggers
-
After-completion logic such as:
-
@future methods
-
Queueable jobs
-
Outbound messaging
-
20. Chatter Feed Triggers
-
Triggers related to feeds and posts.
21. Send Notifications
-
Any push/email notifications are sent after commit.
Best Practices for Handling Order of Execution
-
Use one trigger per object and handle logic via Apex classes.
-
Avoid hardcoding business logic inside triggers.
-
Implement trigger frameworks to control flow.
-
Use recursion guards to prevent infinite loops.
-
Debug using Debug Logs to trace the execution order.
Common Mistakes to Avoid
-
Relying on workflow field updates to update fields used in validation rules.
-
Not accounting for re-execution of validation rules post field updates.
-
Triggering unnecessary flows due to improper condition checks.
-
Using Process Builder and Workflow simultaneously (may lead to unpredictable results).
FAQs on Order of Execution in Salesforce
Q1. Does a workflow field update re-trigger validation rules?
Yes, if a workflow updates fields, Salesforce re-evaluates validation rules and before triggers.
Q2. What is the difference between before and after triggers?
-
Before triggers allow you to update field values before saving to the database.
-
After triggers execute after the record is saved and committed.
Q3. Can a trigger call another trigger?
Yes, indirectly. If a trigger updates a related record, and that update has its own trigger, it will be invoked.
Q4. In what step is Process Builder invoked?
Process Builder runs after workflows but before committing the data.
You may like : Relationships In Salesforce
Conclusion
Understanding the order of execution in Salesforce is a foundational skill for developers and admins alike. It helps ensure data consistency, improves system performance, and avoids logic conflicts. Always design your automation with this execution sequence in mind for scalable and maintainable Salesforce implementations.