All Projects → kevinohara80 → Sfdc Trigger Framework

kevinohara80 / Sfdc Trigger Framework

Licence: mit
A minimal trigger framework for your Salesforce Apex Triggers

Projects that are alternatives of or similar to Sfdc Trigger Framework

Apex Recipes
A library of concise, meaningful examples of Apex code for common use cases following best practices.
Stars: ✭ 307 (-41.75%)
Mutual labels:  apex, salesforce
salesforce-plantuml
Salesforce app to generate UML class & ER-diagrams from your org data. Leverages the PlantUML library.
Stars: ✭ 89 (-83.11%)
Mutual labels:  salesforce, apex
apex-query-builder
Convenient query builder for dynamic SOQL queries
Stars: ✭ 37 (-92.98%)
Mutual labels:  salesforce, apex
ApexTriggerHandler
Another library implements Apex trigger handler design pattern.
Stars: ✭ 40 (-92.41%)
Mutual labels:  salesforce, apex
Script.apex
Evaluate Javascript expressions in Apex
Stars: ✭ 18 (-96.58%)
Mutual labels:  salesforce, apex
fast-sfdc
A VSCode plugin to develop Salesforce projects in vscode
Stars: ✭ 16 (-96.96%)
Mutual labels:  salesforce, apex
amoss
Amoss - Apex Mock Objects, Spies and Stubs - A Simple Mocking framework for Apex (Salesforce)
Stars: ✭ 55 (-89.56%)
Mutual labels:  salesforce, apex
SFDCRules
Simple yet powerful Rule Engine for Salesforce - SFDCRules
Stars: ✭ 38 (-92.79%)
Mutual labels:  salesforce, apex
NebulaFramework
A development framework for Salesforce's Apex language & the Force.com platform
Stars: ✭ 28 (-94.69%)
Mutual labels:  salesforce, apex
apexmock
force.com Mock data and fixtures for Apex Unit Tests
Stars: ✭ 24 (-95.45%)
Mutual labels:  salesforce, apex
APEX-Q
A promise library for Salesforce
Stars: ✭ 30 (-94.31%)
Mutual labels:  salesforce, apex
Lightningflowcomponents
A collection of unofficial Lightning Components that can be used to enhance Salesforce Lightning Flow and Lightning Pages.
Stars: ✭ 252 (-52.18%)
Mutual labels:  apex, salesforce
sf-cross-cutting-concerns
Apex Cross cutting concerns for Salesforce
Stars: ✭ 29 (-94.5%)
Mutual labels:  salesforce, apex
json2apex
Generate strongly typed apex code from a json structure.
Stars: ✭ 121 (-77.04%)
Mutual labels:  salesforce, apex
apex-graphql-query
A library for building GraphQL queries in apex
Stars: ✭ 31 (-94.12%)
Mutual labels:  salesforce, apex
apex-utils
Utility classes for Salesforce Apex development
Stars: ✭ 20 (-96.2%)
Mutual labels:  salesforce, apex
apex-dml-mocking
DML mocking, CRUD mocking, dependency injection framework for Salesforce.com (SFDC) using Apex
Stars: ✭ 38 (-92.79%)
Mutual labels:  salesforce, apex
lwc-modules
Build any LWC you want without ever having to touch Apex
Stars: ✭ 20 (-96.2%)
Mutual labels:  salesforce, apex
apex-fp
Functional programming for Salesforce Apex
Stars: ✭ 231 (-56.17%)
Mutual labels:  salesforce, apex
Ebikes Lwc
Sample application for Lightning Web Components and Communities on Salesforce Platform. Part of the sample gallery. Retail use case. Get inspired and learn best practices.
Stars: ✭ 299 (-43.26%)
Mutual labels:  apex, salesforce

SFDC trigger framework

npm version Maintainability

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

Deploy to Salesforce Org: Deploy

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.

public class OpportunityTriggerHandler extends TriggerHandler {

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.

public class OpportunityTriggerHandler extends TriggerHandler {
  
  public override void beforeUpdate() {
    for(Opportunity o : (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.

public class OpportunityTriggerHandler extends TriggerHandler {

  private Map<Id, Opportunity> newOppMap;

  public OpportunityTriggerHandler() {
    this.newOppMap = (Map<Id, Opportunity>) Trigger.newMap;
  }
  
  public override void afterUpdate() {
    //
  }

}

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.

trigger OpportunityTrigger on Opportunity (before insert, before update) {
  new OpportunityTriggerHandler().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:

public class OpportunityTriggerHandler extends TriggerHandler {

  public OpportunityTriggerHandler() {
    this.setMaxLoopCount(1);
  }
  
  public override void afterUpdate() {
    List<Opportunity> opps = [SELECT Id FROM Opportunity WHERE Id IN :Trigger.newMap.keySet()];
    update opps; // 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:

public class OpportunityTriggerHandler extends TriggerHandler {
  
  public override void afterUpdate() {
    List<Opportunity> opps = [SELECT Id, AccountId FROM Opportunity WHERE Id IN :Trigger.newMap.keySet()];
    
    Account acc = [SELECT Id, Name FROM Account WHERE Id = :opps.get(0).AccountId];

    TriggerHandler.bypass('AccountTriggerHandler');

    acc.Name = 'No Trigger';
    update acc; // won't invoke the AccountTriggerHandler

    TriggerHandler.clearBypass('AccountTriggerHandler');

    acc.Name = 'With Trigger';
    update acc; // will invoke the AccountTriggerHandler

  }

}

If you need to check if a handler is bypassed, use the isBypassed 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 the clearAllBypasses 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()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].