Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A minimal trigger framework for your Salesforce Apex Triggers

License

NotificationsYou must be signed in to change notification settings

kevinohara80/sfdc-trigger-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm versionMaintainability

I know, I know...another trigger framework. Bear with me. ;)

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 singleTriggerHandler 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

Deploy to Salesforce Org:Deploy

Usage

To create a trigger handler, you simply need to create a class that inherits fromTriggerHandler.cls. Here is an example for creating an Opportunity trigger handler.

publicclassOpportunityTriggerHandlerextendsTriggerHandler {

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 abeforeUpdate trigger.

publicclassOpportunityTriggerHandlerextendsTriggerHandler {publicoverridevoidbeforeUpdate() {for(Opportunityo : (List<Opportunity>)Trigger.new) {// do something    }  }// add overrides for other contexts}

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.

publicclassOpportunityTriggerHandlerextendsTriggerHandler {privateMap<Id,Opportunity>newOppMap;publicOpportunityTriggerHandler() {this.newOppMap = (Map<Id,Opportunity>)Trigger.newMap;  }publicoverridevoidafterUpdate() {//  }}

To use the trigger handler, you only need to construct an instance of your trigger handler within the trigger handler itself and call therun() method. Here is an example of the Opportunity trigger.

triggerOpportunityTriggeronOpportunity (beforeinsert,beforeupdate) {newOpportunityTriggerHandler().run();}

Cool Stuff

Max Loop Count

To prevent recursion, you can set a max loop count for Trigger Handler. If this max is exceeded, and exception will be thrown. A great use case is when you want to ensure that your trigger runs once and only once within a single execution. Example:

publicclassOpportunityTriggerHandlerextendsTriggerHandler {publicOpportunityTriggerHandler() {this.setMaxLoopCount(1);  }publicoverridevoidafterUpdate() {List<Opportunity>opps = [SELECTIdFROMOpportunityWHEREIdIN :Trigger.newMap.keySet()];updateopps;// this will throw after this update  }}

Bypass API

What if you want to tell other trigger handlers to halt execution? That's easy with the bypass api:

publicclassOpportunityTriggerHandlerextendsTriggerHandler {publicoverridevoidafterUpdate() {List<Opportunity>opps = [SELECTId,AccountIdFROMOpportunityWHEREIdIN :Trigger.newMap.keySet()];Accountacc = [SELECTId,NameFROMAccountWHEREId = :opps.get(0).AccountId];TriggerHandler.bypass('AccountTriggerHandler');acc.Name ='No Trigger';updateacc;// won't invoke the AccountTriggerHandlerTriggerHandler.clearBypass('AccountTriggerHandler');acc.Name ='With Trigger';updateacc;// will invoke the AccountTriggerHandler  }}

If you need to check if a handler is bypassed, use theisBypassed method:

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 theclearAllBypasses method, as in:

// ... done with bypasses!TriggerHandler.clearAllBypasses();// ... now handlers won't be ignored!

Overridable Methods

Here are all of the methods that you can override. All of the context possibilities are supported.

  • beforeInsert()
  • beforeUpdate()
  • beforeDelete()
  • afterInsert()
  • afterUpdate()
  • afterDelete()
  • afterUndelete()

About

A minimal trigger framework for your Salesforce Apex Triggers

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp