Show: |
HomeSalesforce Trigger FrameworkCreditBased on Kevin O'Hara's famous framework sfdc-trigger-framework DocumentationChangelogOverview(from Kevin O'Hara) 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. Deploy to SFDX Scratch Org: Deploy instructions UsageTrigger HandlerTo 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
A sample AccountSampleTriggerHandler class has been included in this repository, as well as a sample trigger. Note: When referencing the Trigger static maps 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. Technically, you only need to cast for oldMap and newMap, but for completeness, I encourage casting Trigger.new and Trigger.old as well.
Trigger
To use the trigger handler, you only need to construct an instance of your trigger handler within the trigger handler itself and call the This is the way to write a trigger that will run the trigger handlers below. Note that some objects do not work in every context, so ensure that you list only applicable trigger contexts in your trigger definition and that you only override those contexts. If you include extra contexts, they will not be covered by Apex tests, which could lead to deployment problems.
There is also a faster way to write the trigger, specifying the handler class name so the handler does not need to describe the class for its name, saving precious execution time:
Cool StuffBypass APIWhat if you want to tell other trigger handlers to halt execution? That's easy with the bypass api:
Check Bypass StatusIf you need to check if a handler is bypassed, use the
Global BypassTo bypass all handlers, set the global bypass variable:
This will also add an entry 'bypassAll' to the list of handlers returned in To clear all bypasses for the transaction, simply use the
This will clear the list of bypassed handlers and set the Set Bypass
If you are not sure in a transaction if a handler is bypassed, but want to bypass it (or clear the bypass) and then set it to its original value, use the
To store all currently bypassed handlers, temporarily bypass all handlers, and then restore the originally bypassed list:
Max Loop Count
To prevent recursion, you can set a max loop count for Trigger Handler. If this max is exceeded, the trigger will silently stop running. If
In version 1.2, the ability to chain methods was added. Now, you can set the max loop count in a single line in your trigger:
Debug StatementsThere are two methods that will show additional information.
To use one or both of these, either add them to the trigger (note that you can specify for ALL handlers or for a specific handler)
or put them in your Apex trigger handler code before and after DML statements.
In version 1.2, the ability to chain methods in triggers was added. Now, you can specify showing debug messages and limits in a single line in your trigger:
Or stop showing them.
Universal Action
Version 1.1 added a new method/context: Overridable MethodsHere are all of the methods that you can override. All of the context possibilities are supported.
|