- Published on
Salesforce Order of Execution Explained
- Authors
- Name
- Rishabh Sharma
Salesforce Order of Execution Explained
Understanding the order of execution in Salesforce is crucial for developers and admins who want to build reliable, predictable automations and triggers. This guide provides a technical, step-by-step breakdown of what happens when a record is inserted, updated, or deleted in Salesforce.
Step-by-Step: Salesforce Order of Execution
- Load the Original Record (from the database or initialize for new records)
- Load New Record Field Values (from the user or API)
- System Validation Rules (basic checks, required fields, data types)
- Before Triggers (before insert, before update, before delete)
- Custom Validation Rules
- Duplicate Rules
- Savepoint is Set (for rollback)
- After Triggers (after insert, after update, after delete)
- Assignment Rules (e.g., for leads or cases)
- Auto-Response Rules
- Workflow Rules (field updates, tasks, email alerts)
- Processes (Process Builder)
- Escalation Rules (for cases)
- Roll-Up Summary Fields & Cross-Object Updates
- Entitlement Rules (for cases)
- Post-commit Logic (e.g., sending emails, executing asynchronous jobs)
Best Practices for Working with Order of Execution
- Keep Triggers Logic-Free: Use trigger handlers and service classes to avoid complex logic in triggers.
- Avoid Recursive Updates: Use static variables to prevent infinite loops when triggers update records.
- Leverage Before vs. After Triggers: Use before triggers for validation and field updates, after triggers for related record changes and callouts.
- Test for Side Effects: Always test how workflows, processes, and triggers interact—unexpected results can occur if you don’t understand the order.
- Minimize DML and SOQL in Loops: Bulkify your code to handle multiple records efficiently and avoid hitting governor limits.
- Document Automation: Keep a record of all automation on an object to make troubleshooting easier.
Visualizing the Order (Text-Based)
[User/API] → [System Validation] → [Before Triggers] → [Custom Validation] → [After Triggers]
→ [Assignment Rules] → [Auto-Response Rules] → [Workflow/Process Builder] → [Escalation Rules]
→ [Roll-Up Summary] → [Entitlement Rules] → [Post-commit Logic]
By mastering the Salesforce order of execution, you’ll build more robust, maintainable, and scalable automations. Always consider the sequence when designing solutions to avoid conflicts and ensure predictable outcomes.