When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in order.
1 – System Validation rule (SV) – Checks for the Required fields and field format like email should in the correct format.
2 – All Before Triggers are executed (BT) – Before trigger will execute and do the manipulation.
3 – Custom Validation rules are checked (CV) – In this step, Custom validation run and check for the error
4 – Executes duplicate rules (DR) – All duplicate rule will execute for the Object if any
5 – After Triggers are executed (AT)
6 – Assignment Rules are executed (AR)
7 – Auto-Response Rules are executed (ARR)
8 – Workflow Rules are executed (WR)
If workflow rule update any field then before and after triggers are executed one more time if the workflow rule updates a field (BT & AT)
9 – Process Builder (PB)
If process builder update any field then before and after triggers are executed one more time if the workflow rule updates a field (BT & AT)
10 – Escalation Rules are executed (ER)
11 – Parent Rollup Summary Formula or Cross Object Formula fields are updated in the respective objects. (RSF, COF and These parent records also goes through the entire execution order)
12 – Grand Parent Rollup Summary and Cross Object Formula Fields Calculation – ( GPRSF, GPCOF, and Grandparent also go through with the same process).
13 – Criteria Based Sharing rules are evaluated (CBS)
14 – Commits the data into Database!
15 – Any Post-Commit Logic is executed (PCL) (like sending an email)
You can use the below sequence to remember the sequence of the execution and it will help to debug any problem that you will be facing
SV -> BT -> CV -> DR -> AT -> AR -> ARR -> WR (BT, AT) -> PB(BT, AT) -> ER -> RSFCOF -> GP RSFCOF -> -> CBS -> PCL
Please note the following when working with triggers.
The order of execution isn’t guaranteed when having multiple triggers for the same object due to the same event. For example, if you have two before insert triggers for Case, and a new Case record is inserted that fires the two triggers, the order in which these triggers fire isn’t guaranteed.