Experience Salesforce

Trigger Order of Execution

What You’ll Learn

  • Trigger Order of Execution In Apex

Trigger Order of Execution In Apex

Whenever a record is saved with an update, insertion, or upsert statement attached to it, the trigger order of execution takes place in Salesforce. This order is the sequence in which the events will occur per the set criteria. A JavaScript validation also occurs for records containing any dependent picklist fields. This happens before the events occur on the server. The validation limit ensures that the picklist fields are restricted only to the available values.

Trigger order of execution example:

Trigger order of execution example

Salesforce Order of Execution

In Salesforce there is a particular order of execution whenever a record is changed or inserted. 

Following are the steps of trigger execution:

Step 1: The first step is to load an original record from an existing database.

Step 2: Overwrite the old record values with the new values.

If the request is fromValidation check to be performed
A standard UI edit pageCompliance with layout-specific rules
Required values at the layout level and field-definition level
Valid field formats
Maximum field length
A User object on a standard UI edit page,Runs custom validation rules
Multiline item creation such as quote line items and opportunity line itemsRuns custom validation rules
An Apex application or a SOAP API callSalesforce validates only the foreign keys and restricted picklists

Step 3: Execute all before triggers.

Step 4: Run all the user-defined and system validation rules.

Step 5: Save the record to the database but do not commit the record. 

Step 6:  Execute all after triggers.

Step 7: Execute the assignment rules.

Step 8: Execute the auto-response rules.

Step 9: Execute the workflow rules.

Step 10:  If there are workflow field updates then execute the field update.

Step 11: If the record was updated with a workflow field update then execute before and after triggers created on the object in the context again but only once.

Step 12: Execute the processes and flows on that record.

Step 13: Execute the escalation rules.

Step 14: Update the roll-up summary fields & cross-object formula fields.

Step 15: Repeat the same process with the affected parent or grandparent records.

Step 16: Evaluate criteria-based sharing rules.

Step 17: Commit all DML operations to the database.

Step 18: Execute post-commit logic such as sending emails.

Consideration for Trigger Order of Execution

There are some considerations to keep in mind while triggering and order for execution in Salesforce to ensure system performance. 

  • The trigger.old does not go through the workflow of a newly updated field triggered by a record update. For example, if a record field value is 2 and you update this field value to 6, the workflow field update will reflect this change as 8. But in the update trigger that fires after the workflow field update, the field value obtained from Trigger.old is the original value of 2, and not 6.
  • Triggers can take action at each attempt if they are part of the same DML call. This happens due to no change in the transactions and static class variables accessed by the trigger. 
  • In the case of multiple triggers defined for the same event, the order of execution is not defined. 
  • In API version 53.0 and earlier, after-save record-triggered flows run after entitlements are executed.
salesforce-developer
Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale