Академический Документы
Профессиональный Документы
Культура Документы
kevinohara80 / sfdc-trigger-framework
Dismiss
Join GitHub today
GitHub is home to over 40 million developers working together to host and
review code, manage projects, and build software together.
Sign up
README.md
Overview
Triggers should (IMO) be logicless. Putting logic into your triggers creates un-testable, difficult-to-maintain code. It's widely
accepted that a best-practice is to move trigger logic into a handler class.
This trigger framework bundles a single TriggerHandler base class that you can inherit from in all of your trigger handlers.
The base class includes context-specific methods that are automatically called when a trigger is executed.
The base class also provides a secondary role as a supervisor for Trigger execution. It acts like a watchdog, monitoring trigger
activity and providing an api for controlling certain aspects of execution and control flow.
But the most important part of this framework is that it's minimal and simple to use.
https://github.com/kevinohara80/sfdc-trigger-framework 1/4
12/18/2019 GitHub - kevinohara80/sfdc-trigger-framework: A minimal trigger framework for your Salesforce Apex Triggers
Usage
To create a trigger handler, you simply need to create a class that inherits from TriggerHandler.cls. Here is an example for
creating an Opportunity trigger handler.
In your trigger handler, to add logic to any of the trigger contexts, you only need to override them in your trigger handler.
Here is how we would add logic to a beforeUpdate trigger.
Note: When referencing the Trigger statics within a class, SObjects are returned versus SObject subclasses like Opportunity,
Account, etc. This means that you must cast when you reference them in your trigger handler. You could do this in your
constructor if you wanted.
public OpportunityTriggerHandler() {
this.newOppMap = (Map<Id, Opportunity>) Trigger.newMap;
}
To use the trigger handler, you only need to construct an instance of your trigger handler within the trigger handler itself and
call the run() method. Here is an example of the Opportunity trigger.
Cool Stuff
https://github.com/kevinohara80/sfdc-trigger-framework 2/4
12/18/2019 GitHub - kevinohara80/sfdc-trigger-framework: A minimal trigger framework for your Salesforce Apex Triggers
public OpportunityTriggerHandler() {
this.setMaxLoopCount(1);
}
Bypass API
What if you want to tell other trigger handlers to halt execution? That's easy with the bypass api:
TriggerHandler.bypass('AccountTriggerHandler');
TriggerHandler.clearBypass('AccountTriggerHandler');
if (TriggerHandler.isBypassed('AccountTriggerHandler')) {
// ... do something if the Account trigger handler is bypassed!
}
If you want to clear all bypasses for the transaction, simple use the clearAllBypasses method, as in:
TriggerHandler.clearAllBypasses();
Overridable Methods
Here are all of the methods that you can override. All of the context possibilities are supported.
beforeInsert()
beforeUpdate()
beforeDelete()
afterInsert()
https://github.com/kevinohara80/sfdc-trigger-framework 3/4
12/18/2019 GitHub - kevinohara80/sfdc-trigger-framework: A minimal trigger framework for your Salesforce Apex Triggers
afterUpdate()
afterDelete()
afterUndelete()
https://github.com/kevinohara80/sfdc-trigger-framework 4/4