Mytutorialrack

Salesforce Save Order of Execution

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:

  1. Original record is loaded or new record is initialized
  2. Fields values are loaded into sObjects
  3. System validations rules are executed:
  4. Before triggers are executed
  5. System validations rules are run again and custom validation rules are checked.
  6. Duplicate rules are executed
  7. Record is saved but not committed
  8. After triggers are executed
  9. Assignment rules are executed
  10. Auto-response rules are executed
  11. Workflow rules are executed
  12. Before triggers,system validation rules and after triggers are executed due to workflow field updates
  13. Processes are executed
  14. Escalation rules are executed
  15. Entitlement rules are executed
  16. Rull-up summary fields and cross-object formula fields are updated
  17. Updated parent and grand parent records are saved
  18. Criteria based sharing rules are evaluated
  19. DML operations are committed to the database
  20. Post-commit logic is executed

Apex transactions:

  • 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.

Platform Developer 1 Certification Course
References: To learn more
Transaction Control
 

Share:

Recent Posts