SlideShare a Scribd company logo
Triggers and order of execution1
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
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
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.
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).
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
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
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
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.
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.
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
Triggers and order of execution1
Triggers and Order of Execution

More Related Content

PDF
Build Reliable Asynchronous Code with Queueable Apex
PPTX
Batch Apex in Salesforce
PPTX
Salesforce asynchronous apex
PDF
Managing Your Batch and Scheduled Apex Processes with Relax
PPTX
Salesforce DUG - Queueable Apex
PPTX
Episode 19 - Asynchronous Apex - Batch apex & schedulers
PDF
Introduction to the Salesforce Security Model
Build Reliable Asynchronous Code with Queueable Apex
Batch Apex in Salesforce
Salesforce asynchronous apex
Managing Your Batch and Scheduled Apex Processes with Relax
Salesforce DUG - Queueable Apex
Episode 19 - Asynchronous Apex - Batch apex & schedulers
Introduction to the Salesforce Security Model

What's hot (20)

PPTX
Salesforce Basic Development
PPTX
Batchable vs @future vs Queueable
PPTX
How to Use Salesforce Platform Events to Help With Salesforce Limits
PPTX
Data weave 2.0 advanced (recursion, pattern matching)
PDF
Apex Testing Best Practices
PPTX
Episode 6 - DML, Transaction and Error handling in Salesforce
PPTX
Salesforce Development Best Practices
PDF
Lightning web components - Episode 1 - An Introduction
PPTX
Intro to Salesforce Lightning Web Components (LWC)
PPT
Apex Trigger Debugging: Solving the Hard Problems
PPTX
Salesforce Integration Patterns
PPT
Mule salesforce integration solutions
PPTX
Large Data Volume Salesforce experiences
PPTX
Demystify Salesforce Bulk API
PDF
Testing Angular
PPTX
Approval Process in Salesforce
PPTX
Lightning web components
PPTX
Introduction to lightning Web Component
PDF
Lightning web components episode 2- work with salesforce data
PDF
Custom policies in mule 4 and a circuit breaker example
Salesforce Basic Development
Batchable vs @future vs Queueable
How to Use Salesforce Platform Events to Help With Salesforce Limits
Data weave 2.0 advanced (recursion, pattern matching)
Apex Testing Best Practices
Episode 6 - DML, Transaction and Error handling in Salesforce
Salesforce Development Best Practices
Lightning web components - Episode 1 - An Introduction
Intro to Salesforce Lightning Web Components (LWC)
Apex Trigger Debugging: Solving the Hard Problems
Salesforce Integration Patterns
Mule salesforce integration solutions
Large Data Volume Salesforce experiences
Demystify Salesforce Bulk API
Testing Angular
Approval Process in Salesforce
Lightning web components
Introduction to lightning Web Component
Lightning web components episode 2- work with salesforce data
Custom policies in mule 4 and a circuit breaker example
Ad

Viewers also liked (20)

PDF
Introduction to Apex Triggers
PPT
Salesforce Presentation
PPTX
Configuration tips
PPTX
Updating your user profile in communities
PDF
Introduction to Apex Triggers
PPTX
Apex triggers, force_ide_and_deployment
PPTX
Developing Salesforce Console Apps with Visualforce & the Integration Toolkit
PPTX
Brisbane Salesforce User Group - May 2015 - Lightning Process Builder
PDF
Customizing the Salesforce Console With the Integration Toolkit
PDF
Salesforce API: Salesforce Console Deep Dive
DOCX
Approval process
PDF
Introduction to Apex Triggers
PDF
Getting started with Salesforce security
PPTX
Introduction to Salesforce validation rules new
PDF
Exposing Salesforce REST Services Using Swagger
PDF
Salesforce Application Lifecycle Management presented to EA Forum by Sam Garf...
ODP
Workflow in Salesforce
PDF
Salesforce.com API Series: Service Cloud Console Deep Dive
PDF
Best Practices for Team Development in a Single Org
DOCX
Approvals and workflow in salesforce
Introduction to Apex Triggers
Salesforce Presentation
Configuration tips
Updating your user profile in communities
Introduction to Apex Triggers
Apex triggers, force_ide_and_deployment
Developing Salesforce Console Apps with Visualforce & the Integration Toolkit
Brisbane Salesforce User Group - May 2015 - Lightning Process Builder
Customizing the Salesforce Console With the Integration Toolkit
Salesforce API: Salesforce Console Deep Dive
Approval process
Introduction to Apex Triggers
Getting started with Salesforce security
Introduction to Salesforce validation rules new
Exposing Salesforce REST Services Using Swagger
Salesforce Application Lifecycle Management presented to EA Forum by Sam Garf...
Workflow in Salesforce
Salesforce.com API Series: Service Cloud Console Deep Dive
Best Practices for Team Development in a Single Org
Approvals and workflow in salesforce
Ad

Similar to Triggers and order of execution1 (20)

PDF
salesforce triggers interview questions and answers
PPTX
Advanced Topics on Database - Unit-3 AU17
PDF
Salesforce testing best_practices
PPT
Qtp manual testing tutorials by QuontraSolutions
PPTX
Active database
PPTX
Become an Expert in Salesforce Apex Triggers | JanBask Training
PPTX
Control Freak: Risk and Control in Azure DevOps
PPTX
Custom Controllers and Controller Extensions
PPTX
Change tracking
PPTX
LDV.pptx
PPTX
Stored procedures
PDF
Test Driven Development with Sql Server
PPTX
Docker, Zabbix and auto-scaling
PPTX
Data warehouse testing
PDF
Oracle data capture c dc
PPT
Intro to tsql
PPT
Intro to tsql unit 14
PPT
Qtp Training
PPT
Hybrid framework
PDF
Marty, You're Just Not Thinking Fourth Dimensionally
salesforce triggers interview questions and answers
Advanced Topics on Database - Unit-3 AU17
Salesforce testing best_practices
Qtp manual testing tutorials by QuontraSolutions
Active database
Become an Expert in Salesforce Apex Triggers | JanBask Training
Control Freak: Risk and Control in Azure DevOps
Custom Controllers and Controller Extensions
Change tracking
LDV.pptx
Stored procedures
Test Driven Development with Sql Server
Docker, Zabbix and auto-scaling
Data warehouse testing
Oracle data capture c dc
Intro to tsql
Intro to tsql unit 14
Qtp Training
Hybrid framework
Marty, You're Just Not Thinking Fourth Dimensionally

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Complications of Minimal Access Surgery at WLH
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Sports Quiz easy sports quiz sports quiz
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
Complications of Minimal Access Surgery at WLH
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx

Triggers and order of execution1

  • 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
  • 7. 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
  • 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.
  • 13. Triggers and Order of Execution