Triggers and order of execution1

13
Triggers and Order of Execution

Transcript of Triggers and order of execution1

Page 1: Triggers and order of execution1

Triggers and Order of Execution

Page 2: Triggers and order of execution1

Topics to be covered:

1. What are triggers?2. Operations where trigger execute.3. Implementation Considerations4. Types of trigger.5. Trigger syntax and usage .6. Trigger context variables7. Order of execution8. Queries

Page 3: Triggers and order of execution1

Triggers

Definition:

Apex  can be invoked through the use of  triggers.

A trigger is  Apex  code that executes before or after the following types of operations:InsertUpdateDeleteMergeUpsertundelete

Page 4: Triggers and order of execution1

Triggers

Implementation Considerations:

upsert triggers fire both before and after insert or before and after update triggers as appropriate.

merge triggers fire both before and after delete triggers for the losing records and  before update triggers for the winning record only.

Triggers that execute after a record has been undeleted only work with specific objects

Field history is not recorded until the end of a trigger. If you query field history in a trigger, you will not see any history for the current transaction.

Page 5: Triggers and order of execution1

Types of Triggers:

Before triggers are used to update or validate record values before they are saved to the database. A before insert trigger can change the value in the field. We won't get before undelete trigger.

After triggers can be used for a much more complex purposes, like updating a value of some other object when this is updated. Or it can also be used to trigger some other event (like workflow and approval process).

Page 6: Triggers and order of execution1

Triggers

Syntax:

trigger triggerName on ObjectName (trigger_events) { code_block }

Uses for triggers:

Enforce business rulesValidate input dataWrite to other files for audit trail purposesQuery from other files for cross-referencing purposesAccess system functions (for example, print an exception message when a rule is violated)Replicate data to different files to achieve data consistency

Page 7: Triggers and order of execution1

Trigger Context Variables

Triggers define implicit variables that allow developers to access runtime context. •isExecuting•isInsert•isUpdate•isDelete•isBefore•isAfter•isUndelete•New•newMap•Old•oldMap•size

Page 8: Triggers and order of execution1

Order of Execution

When user 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:

a. Compliance with layout-specific rules b. Required values at the layout level and field-definition level c. Valid field formats d. Maximum field length

Page 9: Triggers and order of execution1

Order of Execution

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. But no validation rule of layout-specific is run again.

5. Saves the record to the database, but doesn't commit yet.6. Executes all after triggers.7. Executes assignment rules.8. Executes auto-response rules.9. Executes workflow rules.10.If there are workflow field updates, updates the record again.11.If the record was updated with workflow field updates, fires before and after

triggers one more time, in addition to standard validations. Custom validation rules are not run again.

12.Executes escalation rules.13.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.

Page 10: Triggers and order of execution1

Order of Execution

14. 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.15.Executes Criteria Based Sharing evaluation.16.Commits all DML operations to the database.17.Executes post-commit logic, such as sending email.

Page 11: Triggers and order of execution1

Reference links

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm

http://help.salesforce.com/HTViewHelpDoc?id=code_define_trigger.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

Page 12: Triggers and order of execution1
Page 13: Triggers and order of execution1

Triggers and Order of Execution