In this blog post, we will cover save order of execution in salesforce. We will see the order of events that happen when we save or update a record in salesforce. We will also learn about Apex transactions and the relationship between Apex transaction and order of execution.
Save order of Execution
- The save order of execution is a sequence of events that occur when a record is saved in Salesforce.
- These events are executed when an insert, update or upsert operation occurs.
- All events need to execute successfully before the data is committed to the database.
- In case of a failure, all changes to the database are rolled back and no further events are executed.
Order of Events:
- Original record is loaded or new record is initialized
- Fields values are loaded into sObjects
- System validations rules are executed:
- Before triggers are executed
- System validations rules are run again and custom validation rules are checked.
- Duplicate rules are executed
- Record is saved but not committed
- After triggers are executed
- Assignment rules are executed
- Auto-response rules are executed
- Workflow rules are executed
- Before triggers,system validation rules and after triggers are executed due to workflow field updates
- Processes are executed
- Escalation rules are executed
- Entitlement rules are executed
- Rull-up summary fields and cross-object formula fields are updated
- Updated parent and grand parent records are saved
- Criteria based sharing rules are evaluated
- DML operations are committed to the database
- Post-commit logic is executed
- A single unit of operations in Apex is referred to as an Apex transaction
- Apex transaction boundary can be a trigger, a class method, an anonymous block, a visualforce page or a custom web service method.
- All operations within the transaction boundary and calls that are made to external code represent a single transaction.
- The transaction is committed to the database only after all operations finish executing and don’t cause any errors. In case of an error, all changes are rolled back.
Relationship between Apex transactions and Save order of execution:
- An apex transaction consists of all the apex code that is run during the events in the save order of execution.
- The Apex transaction can consist of multiple DML operations which result in multiple orders of execution.
- The apex transaction ends after changes are committed to the database at the end of the original process.
- There are Apex limits per apex transaction, which may change with each release
- If a governor limit is exceeded, the code terminates with an unrecoverable exception.
- Workflow rules can create recursive loops. For example, if a field update for Rule 1 triggers Rule 2, and a field update for Rule 2 triggers rule 1, it will cause recursion and may cause the organization to exceed its limits of workflow time triggers per hour.
- An apex trigger that has a DML statement which triggers itself will result in an infinite loop and will eventually fail.
- To ensure a trigger only is called once, before the trigger code is executed, a class with a static method/variable can be called to check and set if the code has already been run as part of the transaction.