Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpeleato committed Nov 28, 2023
1 parent b956867 commit d0aff4d
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 22 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0 - 2023-11-27
### Added
- New methods to determine if a specific trigger is enabled and new test methods to cover them.

### Fixed
- Moved recording of trigger execution to the beginning to address a recursion bug.

### Changed
- Updated API versions.
- Moved examples to a top-level folder.
- Updated README.

## 1.0.0 - 2023-09-15
### Added
- Created a new Trigger Framework for Salesforce.
4 changes: 2 additions & 2 deletions examples/classes/AccountTriggerHandler.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
6 changes: 3 additions & 3 deletions examples/classes/AccountTriggerHandlerHelper.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
4 changes: 2 additions & 2 deletions examples/triggers/AccountTrigger.trigger-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexTrigger>
14 changes: 11 additions & 3 deletions force-app/main/default/classes/BaseTriggerHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public abstract class BaseTriggerHandler {

public void run() {
if (this.canRun()) {
this.registerRun();

switch on this.operationType {
when BEFORE_INSERT {
this.beforeInsert();
Expand All @@ -154,8 +156,6 @@ public abstract class BaseTriggerHandler {
this.afterUndelete();
}
}

this.processAfterRun();
}
}

Expand All @@ -176,14 +176,22 @@ public abstract class BaseTriggerHandler {
}

@TestVisible
private void processAfterRun() {
private void registerRun() {
triggerHandlerAndOperation.add(this.triggerHandlerName + this.operationType);
}

//================================================================================
// Static methods to disable/enable trigger execution
//================================================================================

public static Boolean triggerIsEnabled(Type triggerHandler) {
return !disabledTriggerHandlers.contains(triggerHandler);
}

public static Boolean triggerIsEnabled(SObjectType objectType) {
return !disabledTriggerObjects.contains(objectType);
}

public static void disableTriggers(Set<Type> triggerHandlers) {
if (triggerHandlers != null) {
disabledTriggerHandlers.addAll(triggerHandlers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
54 changes: 50 additions & 4 deletions force-app/main/default/classes/BaseTriggerHandlerTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,12 @@ private with sharing class BaseTriggerHandlerTest {
}

@IsTest
static void processAfterRun_onFirstRun_valueAddedToSet() {
static void registerRun_onFirstRun_valueAddedToSet() {
TestTriggerHandler triggerHandler = new TestTriggerHandler();
triggerHandler.operationType = TriggerOperation.BEFORE_INSERT;

Test.startTest();
triggerHandler.processAfterRun();
triggerHandler.registerRun();
Test.stopTest();

String triggerHandlerName = triggerHandler.triggerHandlerName;
Expand All @@ -444,7 +444,7 @@ private with sharing class BaseTriggerHandlerTest {
}

@IsTest
static void processAfterRun_onSecondRun_valueAddedToSet() {
static void registerRun_onSecondRun_valueAddedToSet() {
TestTriggerHandler triggerHandler = new TestTriggerHandler();
triggerHandler.operationType = TriggerOperation.BEFORE_INSERT;

Expand All @@ -453,7 +453,7 @@ private with sharing class BaseTriggerHandlerTest {
BaseTriggerHandler.triggerHandlerAndOperation.add(triggerHandlerName + operationType);

Test.startTest();
triggerHandler.processAfterRun();
triggerHandler.registerRun();
Test.stopTest();

Assert.isTrue(
Expand All @@ -464,6 +464,52 @@ private with sharing class BaseTriggerHandlerTest {

// Static methods to disable/enable trigger execution

@IsTest
static void triggerIsEnabled_passEnabledType_returnTrue() {
Type triggerHandler = TestTriggerHandler.class;

Test.startTest();
Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(triggerHandler);
Test.stopTest();

Assert.isTrue(triggerIsEnabled, 'Expected the trigger "TestTriggerHandler" to be enabled');
}

@IsTest
static void triggerIsEnabled_passDisabledType_returnFalse() {
Type triggerHandler = TestTriggerHandler.class;
BaseTriggerHandler.disabledTriggerHandlers.add(triggerHandler);

Test.startTest();
Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(triggerHandler);
Test.stopTest();

Assert.isFalse(triggerIsEnabled, 'Expected the trigger "TestTriggerHandler" to be disabled');
}

@IsTest
static void triggerIsEnabled_passEnabledObject_returnTrue() {
SObjectType objectType = Account.SObjectType;

Test.startTest();
Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(objectType);
Test.stopTest();

Assert.isTrue(triggerIsEnabled, 'Expected the trigger for Account to be enabled');
}

@IsTest
static void triggerIsEnabled_passDisabledObject_returnFalse() {
SObjectType objectType = Account.SObjectType;
BaseTriggerHandler.disabledTriggerObjects.add(objectType);

Test.startTest();
Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(objectType);
Test.stopTest();

Assert.isFalse(triggerIsEnabled, 'Expected the trigger for Account to be disabled');
}

@IsTest
static void disableTriggers_passSetOfTypePopulated_disabledTriggerHandlersPopulated() {
Set<Type> triggerHandlers = new Set<Type>{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
4 changes: 2 additions & 2 deletions force-app/main/default/classes/TriggerUtil.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
6 changes: 3 additions & 3 deletions force-app/main/default/classes/TriggerUtilTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>

0 comments on commit d0aff4d

Please sign in to comment.