What is the Order of Execution for a Salesforce Trigger?
Understanding the Salesforce order of execution is critical for building reliable Apex triggers and avoiding unexpected behavior. The platform runs a well-defined sequence when a record is saved. Below is a concise, SEO-friendly breakdown you can reference during interviews or while designing trigger logic.
High-level overview
When a DML operation (insert, update, delete, undelete) occurs, Salesforce executes a series of steps in a deterministic order. Keeping triggers lean and delegating complex logic to handler classes helps maintain predictable behavior.
Detailed order of execution
Follow these steps in order (simplified and numbered for clarity):
1. System Validation (format, required fields)
2. Before triggers
3. Custom validation rules
4. Duplicate rules
5. Save to database (but not committed yet)
6. After triggers
7. Assignment rules
8. Auto-response rules
9. Workflow rules (and immediate field updates)
10. If workflow field updates occur: run before & after triggers again (only for updated records)
11. Escalation rules
12. Roll-up summary and cross-object changes
13. Criteria-based sharing evaluation
14. Commit to database
15. Post-commit asynchronous processes (email, outbound messages)
16. Queueable, future, batch, and scheduled jobs (asynchronous)
Notes and important caveats
– Workflow field updates can cause triggers to re-fire. If a workflow updates a field, Salesforce re-runs before and after triggers for that record (but only one additional cycle per save).
– Validation rules and duplicate rules run before records are committed. If they fail, the entire transaction is rolled back.
– Assignment, auto-response, and escalation rules run after the record is saved (but before commit) and can invoke external functionality.
– Roll-up summary fields are recalculated when child records are changed; this can cause parent triggers to execute.
– Asynchronous processes (future, queueable, batch, scheduled) run after the commit. Use them for long-running or non-blocking tasks.
Best practices for triggers
- Use trigger frameworks and a single trigger per object to avoid order confusion.
- Keep logic out of triggers — use Apex handler classes and service layers.
- Avoid DML or callouts in loops to prevent governor limits.
- Be cautious with workflow rules and processes that update fields — they may cause recursive executions.
- Write unit tests for scenarios with workflow field updates, validation failures, and roll-up updates to ensure deterministic behavior.
Quick reference snippet
Copy/pasteable summary you can memorize:
DML Begin -> System Validation -> Before Triggers -> Validation Rules -> Duplicate Rules -> Save (but not committed) -> After Triggers -> Assignment/Auto-response/Workflow -> If Workflow Field Update -> Before & After Triggers (again) -> Escalation -> Roll-up Summary -> Commit -> Post-commit (async)
Mastering this order will help you design predictable triggers and troubleshoot complex save-time issues in Salesforce.








Leave a Reply