This project is meant to demonstrate an Apex Trigger Framework which is built with the following goals in mind:
- Single Trigger per sObject
- Logic-less Triggers
- Context Specific Implementation
- Easy to Migrate Existing Code
- Simple Unit Testing
- Configuration from Setup Menu
- Adherance to SOLID Principles
In order to use this trigger framework, we start with the MetadataTriggerHandler class which is included in this project.
Trigger OppportunityTrigger on Opportunity (before insert, after insert, before update, after update, before delete, after delete, after undelete) {
new MetadataTriggerHandler().run();
}To define a specific action, we write an individual class which implements the correct context interface.
public class ta_Opportunity_StageInsertRules implements TriggerAction.BeforeInsert {
@TestVisible
private static final String INVALID_STAGE_INSERT_ERROR = 'The Stage must be \'' + Constants. OPPORTUNITY_STAGENAME_PROSPECTING + '\' when an Opportunity is created';
public void beforeInsert(List<Opportunity> newList){
for (Opportunity opp : newList) {
if (opp.StageName != Constants.OPPORTUNITY_STAGENAME_PROSPECTING) {
opp.addError(INVALID_STAGE_INSERT_ERROR);
}
}
}
}This allows us to use custom metadata to configure a few things from the setup menu:
- The sObject and context for which an action is supposed to execute
- The order to take those actions within a given context
- A checkbox to bypass execution at the sObject or trigger action level
The setup menu provides a consolidated view of all of the actions that are executed when a record is inserted, updated, deleted, or undeleted.
The MetadataTriggerHandler class fetches all Trigger Action metadata that is configured in the org, and dynamically creates an instance of an object which implements a TriggerAction interface and casts it to the appropriate interface as specified in the metadata, then calls their respective context methods in the order specified.
Now, as future development work gets completed, we won't need to keep modifying the bodies of our triggerHandler classes, we can just create a new class for each new piece of functionality that we want and configure those to run in a specified order within a given context.
Note that if an Apex class is specified in metadata and it does not exist or does not implement the correct interface, a runtime error will occur.
With this multiplicity of Apex classes, it would be wise to follow a naming convention such as ta_ObjectName_Description and utilize the sfdx-project.json file to partition your application into multiple directories.
{
"packageDirectories": [
{
"path": "application/base",
"default": true
},
{
"path": "application/opportunity-automation",
"default": false
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "50.0"
}Use the TriggerBase.idsProcessedBeforeUpdate and TriggerBase.idsProcessedAfterUpdate to prevent recursively processing the same record(s).
public class ta_Opportunity_RecalculateCategory implements TriggerAction.AfterUpdate {
public void afterUpdate(List<Opportunity> newList, List<Opportunity> oldList) {
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(oldList);
List<Opportunity> oppsToBeUpdated = new List<Opportunity>();
for (Opportunity opp : newList) {
if (
!TriggerBase.idsProcessedAfterUpdate.contains(opp.id) &&
opp.StageName != oldMap.get(opp.id).StageName
) {
oppsToBeUpdated.add(opp);
}
}
if (!oppsToBeUpdated.isEmpty()) {
this.recalculateCategory(oppsToBeUpdated);
}
}
private void recalculateCategory(List<Opportunity> opportunities) {
//do some stuff
update opportunities;
}
}You can also bypass execution on either an entire sObject, or for a specific action.
To bypass from the setup menu, simply navigate to the sObject Trigger Setting or Trigger Action metadata record you are interested in and check the Bypass Execution checkbox.
These bypasses will stay active until the checkbox is unchecked.
To bypass from Apex, use the static bypass(String actionName) method in the MetadataTriggerHandler class, or the static bypass(String sObjectName) method in the TriggerBase class.
public void updateAccountsNoTrigger(List<Account> accountsToUpdate) {
TriggerBase.bypass('Account');
update accountsToUpdate;
TriggerBase.clearBypass('Account');
}public void insertOpportunitiesNoRules(List<Opportunity> opportunitiesToInsert) {
MetadataTriggerHandler.bypass('ta_Opportunity_StageInsertRules');
insert opportunitiesToInsert;
MetadataTriggerHandler.clearBypass('ta_Opportunity_StageInsertRules');
}These bypasses will stay active until the transaction is complete or until cleared using the clearBypass or clearAllBypasses methods in the TriggerBase and MetadataTriggerHandler classes.
To avoid having to downcast from Map<Id,sObject>, we simply construct a new map out of our newList and oldList variables:
public void beforeUpdate(List<Opportunity> newList, List<Opportunity> oldList) {
Map<Id,Opportunity> newMap = new Map<Id,Opportunity>(newList);
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(oldList);
...
}This will help the transition process if you are migrating an existing Salesforce application to this new trigger actions framework.
Peforming DML operations is extremely computationally intensive and can really slow down the speed of your unit tests. We want to avoid this at all costs. Traditionally, this has not been possible with existing Apex Trigger frameworks, but this Trigger Action approach makes it much easier. Included in this project is a TestUtility class which allows us to generate fake record Ids.
@IsTest
public class TestUtility {
static Integer myNumber = 1;
public static Id getFakeId(Schema.SObjectType sObjectType) {
String result = String.valueOf(myNumber++);
return (Id)(sObjectType.getDescribe().getKeyPrefix() + '0'.repeat(12-result.length()) + String.valueOf(myNumber++));
}
}We can also use getErrors() method to test the addError(errorMsg) method of the SObject class.
Take a look at how both of these are used in the ta_Opportunity_StageChangeRulesTest class:
@IsTest
private static void beforeUpdate_test() {
List<Opportunity> newList = new List<Opportunity>();
List<Opportunity> oldList = new List<Opportunity>();
//generate fake Id
Id myRecordId = TestUtility.getFakeId(Opportunity.SObjectType);
newList.add(new Opportunity(Id = myRecordId, StageName = Constants.OPPORTUNITY_STAGENAME_CLOSED_WON));
oldList.add(new Opportunity(Id = myRecordId, StageName = Constants.OPPORTUNITY_STAGENAME_QUALIFICATION));
Test.startTest();
new ta_Opportunity_StageChangeRules().beforeUpdate(newList, oldList);
Test.stopTest();
//Use getErrors() SObject method to get errors from addError without performing DML
System.assertEquals(true, newList[0].hasErrors());
System.assertEquals(1, newList[0].getErrors().size());
System.assertEquals(
newList[0].getErrors()[0].getMessage(),
String.format(
ta_Opportunity_StageChangeRules.INVALID_STAGE_CHANGE_ERROR,
new String[] {
Constants.OPPORTUNITY_STAGENAME_QUALIFICATION,
Constants.OPPORTUNITY_STAGENAME_CLOSED_WON
}
)
);
}Notice how we performed zero DML operations yet we were able to cover all of the logic of our class in this particular test. This can help save a lot of computational time and allow for much faster execution of Apex tests.



