Describe the relationship between Apex transactions, the save order of execution, and the potential for recursion and/or cascading.

After studying this topic, you should be able to:

  • Describe how the Save Order of Execution is utilized in Salesforce
  • List the various steps in the Save Order of Execution
  • Define and explain the functionality of an Apex Transaction
  • Describe how the Save Order of Execution is related to Apex Transactions
  • Describe the impact of cascading triggers and recursion due to workflow field updates

Table of Contents

  • Save Order of Execution
  • Apex Transactions
  • Recursion and Cascading

Introduction

  • This topic explains:
    • The relationship between Apex transactions and the save order of execution
    • The potential for cascading and recursion due to triggers and workflow rules
  • Relationship between execution order and execution context can help resolving issues that occur as a result of the events
  • Cascading and recursion due to triggers should be taken into consideration when creating triggers and workflow rules to prevent potential issues with records

Save Order of Execution

  • Save Order of Execution: sequence of events that occur when a record is saved in Salesforce
    • Execution: events are executed when an insert, update, or upsert database operation occurs
    • Commit: all events should execute successfully before the change is committed to the database
    • Failure: in case of a failure, all changes to the database are rolled back and no further events are executed
  • Order of Events in the save order of execution:
    1. Original record loaded or new record initialized
    2. New field values are loaded and old field values are overwritten. Some system validation rules are run
    3. Before-save record-triggered flows are executed
    4. All before triggers are executed
    5. Most system validation rules are run again, and any custom validation rules are executed
    6. Duplicate rules are executed
    7. Record is saved to the database, but not committed
    8. All after triggers are executed
    9. Assignment rules are executed
    10. Auto-response rules are executed
    11. Workflow rules are executed. Additional operations may be run due to field updates
    12. Escalation rules are executed
    13. Processes as well as flows that are launched by processes are executed
    14. After-save record-triggered flows are executed
    15. Entitlement rules are executed
    16. Parent record is updated if the record contains a rolled-up field or is part of a cross-object workflow
    17. Grandparent record may be updated for the same reasons the parent record was updated
    18. Evaluation of criteria-based sharing rules is executed
    19. All DML operations are committed to the database
    20. Post-commit logic is executed

Notes

  • Step 2: if save request was performed from standard UI edit page, some system validation rules run (required fields, max field length, etc). If save request came from other sources, Salesforce validates foreign keys. If line items were created (such as opportunity line items), custom validation rules are also run.
  • Step 11: if the workflow rule includes a field update, it may again update the record, run system validations, or execute before update and after update triggers one last time
    • In Spring ‘22, field updates that are executed by approvals and time-dependent actions in workflow rules invoke before-save and after-save record-triggered flows
  • Step 20: post-commit logic includes operations like sending emails and execution of enqueued asynchronous Apex jobs (queueable jobs, future methods, etc) and outbound messages

Apex Transactions

  • Apex transactions are a single unit of operations
    • Boundary: Apex transaction boundard can be a trigger, a class method, an anonymous block, a Visualforce page, or a custom Web service method
    • Single Transaction: all operations within the transaction boundary and calls that are made to external code represent a single transaction
    • Commit: the transaction is committed to the database only after all operations finish executing and don’t cause any errors. All changes are rolled back in case of an error.

Recursion and Cascading

  • Apex triggers and workflow rules can result in cascading and recursion
    • Recursion: same record could be modified multiple times during the same transaction
    • Cascading: automations can potentially modify other records of the same sObject type or records of other sObjects, which can cause other automations to execute
    • Recursive Loop: workflow rules can cause recursive loops.
      • Ex: field update for Rule1 triggers Rule2, then Rule2 triggers Rule1: recursion. This may cause the orgnization to exceed its limit for workflow time triggers per hour
    • Infinite loop: Apex trigger that has a DML statement that triggers itself will result in an infinite loop and eventually fail
    • Single execution: to ensure a trigger is called only 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
    • Maximum stack depth: this is a limit on the recursive firing of Apex triggers
      • Max stack depth for any Apex invocation that recursively fires insert, update, or delete triggers is 16
    • Workflow field updates: if a workflow field update updates a record, before and after triggers are fired one more time
      • This applies to all types of DML operations

Recursive Save: Data may be saved multiple times in an a single transaction, but a database commit is executed one time only. Note that steps 9-17 are skipped in a recursive save.

Cascaded Operation: Cascading may occur in a transaction and is a common scenario. For example, an after trigger (step 8) may perform an update DML statement for another record, which initiates another order of execution.

Parent and Grandparent Records: When a parent/grandparent is updated due to its roll-up summary fields or cross-object field updates, an entire order of execution will be performed for each record, but excluding the database commit

Recursion and Cascading: following image illustrates recursive or infinite loop scenarios to avoid. Be aware of Cascading between different tools on the Salesforce platform as well.

Error from Infinite Loop: Apex triggers that triggers itself will result in an infinite loop that will eventually fail, possibly resulting in an error like that shown below.