Path to Code
Begin Your Salesforce Coding Adventure
Episode 20
Trigger Frameworks in Salesforce
• Enterprise Solutions Architect
• Global Community Speaker
Jigar Shah
@jigarshah189
Agenda
• Need for Trigger Frameworks
• Design Patterns for Trigger Frameworks
• Benefits for Trigger Frameworks
• Choosing the right Trigger Framework
• Q&A
Some Housekeeping Rules…
• Mute your mic
• Keep adding questions in Zoom Q&A Window
• No questions are silly!
• Questions will be answered in the last 15 mins of this session
Apex Triggers
• Automatic execution of Apex code responding to Database operations
 Insert, Update, Delete, Undelete
 Before / After a record save
• Most Standard and All Custom Objects
Order of Execution
1. Loads the original record from the database
2. Loads the new record field values from a request and overwrites old values
3. System Validations
4. Custom Validation Rules
5. Before Triggers
6. Duplicate Rules
7. Saves the record to the database but not committed. Record ID is generated
8. After Triggers
9. Assignment Rules execution
10. Auto-response Rules execution
11. Workflow Rules execution
12. Execute Processes
13. Execute Flows
14. Executes Escalation Rules
15. Executes Entitlement Rules
16. Update roll-up summary fields
17. Criteria based sharing evaluation
18. Commits all DML operations to the database
19. Executes post commit logic of sending emails
Demo Use Case
 Create Task for any Opportunity when it is inserted or updated with
a stage of “Closed Won”.
 The Task’s Subject must be set to “Follow Up Test Task”
Demo 1
Triggers with Business Logic
Why Trigger Frameworks?
 Inconsistent orchestration for Apex trigger logic
 Increased complexity of unit testing
 Code maintenance is complex and difficult
 Difficult for disparate developers to work together on a single code base
Trigger Handler Pattern
sObject
<sObject>Trigger <sObject>TriggerHandler
Business Logic 1
Business Logic N
One Trigger per sObject Delegator
Demo 2
Trigger Handler Framework
Interface based Trigger Framework
sObject
<sObject>Trigger
<sObject>TriggerHandler
Business Logic
ITriggerHandler
TriggerDispatcher
run (ITriggerHandler handler)
implements
Interface
calls
calls
Dynamic Binding
Demo 3
Interface based Trigger Framework
Benefits of Trigger Frameworks
 Consistent way of writing and executing triggers
 Predictable order of execution with a single trigger per object
 Prevent recursive trigger execution
 Promotes code maintainability and easy unit testing
 Cohesive disparate teams with better collaboration
Choosing the Right Trigger Framework
Features Trigger Handler Interface Based
Complexity Simple Complex
1 Trigger per sObject *Partially Yes
Extensible No Yes
Code Reusability Low High
Recursion Control No Yes
Resources
 Lightweight Apex Trigger Framework by Chris Aldridge (Blog)
 Minimal SFDC Trigger Framework by Kevin O'Hara (Blog)
 Tidy Streamlined Bulkified Triggers (Dev Cookbook)
 Trigger Frameworks in Salesforce (YouTube)
Trailhead Modules
Apex Triggers
Q & A
Thank You
Subscribe

Episode 20 - Trigger Frameworks in Salesforce

  • 1.
    Path to Code BeginYour Salesforce Coding Adventure
  • 2.
  • 3.
    • Enterprise SolutionsArchitect • Global Community Speaker Jigar Shah @jigarshah189
  • 4.
    Agenda • Need forTrigger Frameworks • Design Patterns for Trigger Frameworks • Benefits for Trigger Frameworks • Choosing the right Trigger Framework • Q&A
  • 5.
    Some Housekeeping Rules… •Mute your mic • Keep adding questions in Zoom Q&A Window • No questions are silly! • Questions will be answered in the last 15 mins of this session
  • 6.
    Apex Triggers • Automaticexecution of Apex code responding to Database operations  Insert, Update, Delete, Undelete  Before / After a record save • Most Standard and All Custom Objects
  • 7.
    Order of Execution 1.Loads the original record from the database 2. Loads the new record field values from a request and overwrites old values 3. System Validations 4. Custom Validation Rules 5. Before Triggers 6. Duplicate Rules 7. Saves the record to the database but not committed. Record ID is generated 8. After Triggers 9. Assignment Rules execution 10. Auto-response Rules execution 11. Workflow Rules execution 12. Execute Processes 13. Execute Flows 14. Executes Escalation Rules 15. Executes Entitlement Rules 16. Update roll-up summary fields 17. Criteria based sharing evaluation 18. Commits all DML operations to the database 19. Executes post commit logic of sending emails
  • 8.
    Demo Use Case Create Task for any Opportunity when it is inserted or updated with a stage of “Closed Won”.  The Task’s Subject must be set to “Follow Up Test Task”
  • 9.
    Demo 1 Triggers withBusiness Logic
  • 10.
    Why Trigger Frameworks? Inconsistent orchestration for Apex trigger logic  Increased complexity of unit testing  Code maintenance is complex and difficult  Difficult for disparate developers to work together on a single code base
  • 11.
    Trigger Handler Pattern sObject <sObject>Trigger<sObject>TriggerHandler Business Logic 1 Business Logic N One Trigger per sObject Delegator
  • 12.
  • 13.
    Interface based TriggerFramework sObject <sObject>Trigger <sObject>TriggerHandler Business Logic ITriggerHandler TriggerDispatcher run (ITriggerHandler handler) implements Interface calls calls Dynamic Binding
  • 14.
    Demo 3 Interface basedTrigger Framework
  • 15.
    Benefits of TriggerFrameworks  Consistent way of writing and executing triggers  Predictable order of execution with a single trigger per object  Prevent recursive trigger execution  Promotes code maintainability and easy unit testing  Cohesive disparate teams with better collaboration
  • 16.
    Choosing the RightTrigger Framework Features Trigger Handler Interface Based Complexity Simple Complex 1 Trigger per sObject *Partially Yes Extensible No Yes Code Reusability Low High Recursion Control No Yes
  • 17.
    Resources  Lightweight ApexTrigger Framework by Chris Aldridge (Blog)  Minimal SFDC Trigger Framework by Kevin O'Hara (Blog)  Tidy Streamlined Bulkified Triggers (Dev Cookbook)  Trigger Frameworks in Salesforce (YouTube)
  • 18.
  • 19.
  • 20.
  • 21.