Activity Forums Salesforce® Discussions What is the order of operation for triggers in Salesforce?

  • Nikita

    Member
    August 14, 2019 at 4:41 am

    Hi Piyush,

    When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in order.

    1. Loads the original record from the database or initializes the record for an upsert statement.
    2. Loads the new record field values from the request and overwrites the old values. If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:
      • Compliance with layout-specific rules
      • Required values at the layout level and field-definition level
      • Valid field formats
      • Maximum field length
      • When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Before executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself. Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.
    3. Executes all before triggers.
    4. Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
    5. Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
    6. Saves the record to the database, but doesn't commit yet.
    7. Executes all after triggers.
    8. Executes assignment rules.
    9. Executes auto-response rules.
    10. Executes workflow rules.
    11. If there are workflow field updates, updates the record again.
    12. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.
    13. Executes processes and flows launched via processes and flow trigger workflow actions.When a process or flow executes a DML operation, the affected record goes through the save procedure.
    14. Executes escalation rules.
    15. Executes entitlement rules.
    16. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through the save procedure.
    17. If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through the save procedure.
    18. Executes Criteria Based Sharing evaluation.
    19. Commits all DML operations to the database.
    20. Executes post-commit logic, such as sending email.
  • Saddam

    Member
    August 14, 2019 at 6:28 am

    Hi Piyush

    When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in order.

    Before Salesforce executes these events on the server, the browser runs JavaScript validation if the record contains any dependent picklist fields. The validation limits each dependent picklist field to its available values. No other validation occurs on the client side.

    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 (CV, BT & AT)

    Note: – Custom Validation executes when there is any update is being             made by process builder.

    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(CV, BT, AT)  -> ER -> RSFCOF ->  GP RSFCOF  -> -> CBS -> PCL

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos