TRIGGERS AND ORDER OF EXECUTION.
- This topic came up again today in reference to a trigger to modify lead assignments. It’s always important to keep the order of these events in mind when developing as it can cause unintended consequences and a debugging nightmare.
- When a record is saved with an insert, update, or upsert statement, the following events occur in order:
The original record is loaded from the database (or initialized for an insert statement)The new record field values are loaded from the request and overwrite the old values:
1. System Validation Rules.
2. Executes all before triggers.
3. Custom Validation rules.
4. Executes all after triggers.
5. Executes assignment rules.
6. Executes auto-response rules.
7. Executes workflow rules.
8. If there are workflow field updates, updates the record again.
9. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.
10. Executes escalation rules.
11. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
12. If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
13. Executes Criteria Based Sharing evaluation.
14. Commits all DML operations to the database.
15. Executes post-commit logic. Ex: Sending email.