9

Trigger Order of Execution

Trigger Order of Execution In Apex

Whenever a record is saved with an update, insert, or upsert statement attached, the trigger execution order in Salesforce is determined. This order is the sequence in which the events will occur per the set criteria. Client-Side validation also occurs for records that contain 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. The following are the steps of trigger execution:

Step 1: 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 rules that result in field updates. Execute the field updates.

Step 11: If the record was updated via a workflow field update, execute the before and after triggers 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 things to keep in mind during the trigger order execution to ensure system performance. 

  • The trigger.old does not run the workflow for a newly updated field triggered by a record update. For example, if a record field value is 2 and you update it to 6, the workflow field update will reflect the change as 6 . 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 because there is no change to the transactions or to the static class variables accessed by the trigger. 
  • When multiple triggers are defined for the same event, the execution order 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 more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Frequently Asked Questions

What is the order of execution in Salesforce?

The order of execution is a defined, specific sequence of events when a record is saved in Salesforce.

What is the difference between before and after triggers?

Before triggers run before any record is saved to the database. This makes it ideal for validating or modifying fields before saving. The after triggers run once a record is saved. It allows actions that affect the record’s saved state, like creating related records or sending notifications.

Do workflows trigger triggers again?

Yes, if any workflow field is updated, the record will be modified, and the before- and after-triggers will fire again, but Salesforce allows this process only once.

Is the order of execution always fixed?

The basic sequence of execution steps is always fixed. If multiple triggers for the same event on the same object are present, the order is not guaranteed.

Book Free15-Minutes Career Counselling