Salesforce Apex Trigger Best Practices

TRIGGERS AND ORDER OF EXECUTION.

  • This topic came up again today in reference to a trigger to modify lead assignments. It’s always important to keep the order of these events in mind when developing as it can cause unintended consequences and a debugging nightmare.
  • When a record is saved with an insert, update, or upsert statement, the following events occur in order:

The original record is loaded from the database (or initialized for an insert statement)The new record field values are loaded from the request and overwrite the old values:

1. System Validation Rules.

2. Executes all before triggers.

3. Custom Validation rules.

4. Executes all after triggers.

5. Executes assignment rules.

6. Executes auto-response rules.

7. Executes workflow rules.

8. If there are workflow field updates, updates the record again.

9. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.

10. Executes escalation rules.

11. 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 save procedure.

12. If the parent record is updated, and a grand-parent 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. Grand-parent record goes through save procedure.

13. Executes Criteria Based Sharing evaluation.

14. Commits all DML operations to the database.

15. Executes post-commit logic. Ex: Sending email.

Blog Resources:
http://blog.jeffdouglas.com/2009/03/06/triggers-and-order-of-execution/
http://www.infallibletechie.com/2013/04/order-of-execution-in-salesforce.html

0 Comments

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Property of Forcetalks.com
Copyright 2017 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?