Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Â
Triggers and order of execution1
1.
2. Topics to be covered:
1. What are triggers?
2. Operations where trigger execute.
3. Implementation Considerations
4. Types of trigger.
5. Trigger syntax and usage .
6. Trigger context variables
7. Order of execution
8. Queries
3. 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:
ďąInsert
ďąUpdate
ďąDelete
ďąMerge
ďąUpsert
ďąundelete
4. 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.
5. 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).
6. Triggers
Syntax:
trigger triggerName on ObjectName (trigger_events) { code_block }
Uses for triggers:
ď§Enforce business rules
ď§Validate input data
ď§Write to other files for audit trail purposes
ď§Query from other files for cross-referencing purposes
ď§Access system functions (for example, print an exception message when a rule is violat
ď§Replicate data to different files to achieve data consistency
8. 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
9. 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.
10. Order of Execution
14. If the parent record is updated, and a grand-parent record contains a roll-up summa
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.