What is the order of execution in trigger ?
As we all know that in salesforce all the operations on a particular record is executed on an order and not randomly. These operations might be an insert,update, delete and upsert and can be done by workflow, process builder, trigger or by any other means. So, in the same way apex trigger execution also follows a definite order based on that DML operation performed on record. Following are the order of execution :-
First thing first, loads the original records from the salesforce database or initialised the record
Salesforce loads the new record field values from the request and update the old values
Now salesforce executes all before triggers (which all applicable for the object)
Now salesforce runs the internal checks (validation) steps again to confirm that all required fields have a non-null value and runs if any user defined validation checks present in the system. The layout specific rules or the request which comes from standard UI edit page ) is not run second time by salesforce.
Salesforce runs the duplicates rules if any present in salesforce or defined by user. The duplicates rules mainly defined by users to find duplicates records present in salesforce application.
Now the records are saved in Database but yet to commit.
Now salesforce will execute all after triggers.
Now Salesforce executes assignment rules if any present
Now salesforce execute all auto-response rules if any
Now salesforce will execute workflow rules
If there are any workflow which responsible to update a field then record update will happen again
If any record was updated with workflow field updates, salesforce will fire before update and after update trigger one more time. This will run only once but in some scenarios it will create trigger recursive issue(We discuss this in detail in a later section).
Now salesforce will run process and flows launched via process builder and flow trigger workflow actions.
Now salesforce will run escalation rules.
Now salesforce will run entitlements rules