Tuesday, October 7, 2014

Order of Execution of Salesforce's Trigger

When a record is saved with an insert, update, or upsert statement, the following events occur in order:
  1. The original record is loaded from the database or initialized for in case of an insert statement.
  2. The new record field values are loaded from the request and overwrite the old values.
  3. System validation occurs, such as verifying that all required fields have a non-null value, and running any user-defined validation rules.
  4. All before triggers execute.
  5. The record is saved to the database, but not yet committed.
  6. All after triggers execute.
  7. Assignment rules execute.
  8. Auto-response rules execute.
  9. Workflow rules execute.
  10. If there are workflow field updates, the record is updated again.
  11. If the record was updated with workflow field updates, before and after triggers fire one more time (and only one more time).
  12. Escalation rules execute.
  13. All DML operations are committed to the database.
  14. Post-commit logic executes, such as sending email.
Here is the diagram -

The Pdf version is here. Click Here
It is always important to keep the order of execution in mind. Please provide your feedback.

2 comments:

  1. Hi Sudipta,

    I have just started learning sfdc. I find this an interesting article, I have a small doubt in this order of execution,
    in step no. 2 ('The new record field values are loaded from the request and overwrite the old values.') I think you are referring here to the values that are updated and Ignored in case of 'insert'.

    Please correct me if I am wrong, help appreciated.

    Thanks,
    Noupada

    ReplyDelete
    Replies
    1. Yes Noupada -

      Please find the detail description below -
      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
      Salesforce doesn't perform system validation in this step when the request comes from other sources, such as an Apex application or a SOAP API call.
      Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.

      Let me know if you need any help. Thanks.

      Delete