|

Salesforce Development Tutorial: How to Implement a Trigger Framework

Why use a Trigger Framework?

There are a couple big reasons why you would want to implement a trigger handler. Let’s check them out below:

1) It makes your triggers way easier to test. By putting your logic in your trigger handler, instead of your trigger you allow yourself a much simpler path to testing your trigger. You don’t have to do tons of real inserts on the object to test the logic within the trigger, you could create mock objects and call the trigger handler class to test the logic in your trigger instead (test mocking is a wonderful thing that saves you tons of time, but we’re not covering that today).

2) It allows your trigger code to be leveraged in other places. Maybe your trigger handler code could be used in a lightning web component, aura component, a batch class, etc. By creating this code in a trigger handler (or by having that handler call a utility class), you have the option for other code to utilize it as well, reducing the overall code in your org.

Which Trigger Framework should I use?

There are tons of great trigger frameworks, but my personal favorite is the sfdc-trigger-framework. I prefer it because it’s super lightweight, you get the benefits I mentioned above, it’s easy to extend upon it if you need to and it’s not super complicated to learn.

This allows you to get your teams on board and using the framework super quick and the ease of extensibility allows you add whatever extra functionality you need without it getting super confusing and difficult.

How to use the Framework?

After you’ve download the TriggerHandler class from the sfdc-trigger-framework repo and put it in your org, it’s actually pretty simple, but let’s just take a look. We’ll build the trigger handler class for the case object first:

public with sharing class Case_Trigger_Handler extends TriggerHandler 
{
    public override void beforeInsert()
    {
        //Logic/method calls here
    }    
}

So what’s goin on above? Basically what we’ve done is created an apex class that extends the TriggerHandler class so that it can benefit from all the accessible methods within it through the concept of inheritance. We’re able to override the beforeInsert virtual method in the TriggerHandler class to give it logic that is unique to the case trigger. Pretty kewl huh?

Now let’s check out the actual Case Trigger.

trigger Case_Trigger on Case (before insert) 
{
    new Case_Trigger_Handler().run();
}

Wow… there is almost nothing there… wyld. Basically what we’re doing is creating an instance of the Case_Trigger_Handler class and calling the run method that it inherited from the TriggerHandler class it extends. We can check out that method below:

// main method that will be called during execution
    public void run() {
  
      if(!validateRun()) {
        return;
      }
  
      addToLoopCount();
  
      // dispatch to the correct handler method
      switch on this.context {
        when BEFORE_INSERT {
          this.beforeInsert();
        }
        when BEFORE_UPDATE {
          this.beforeUpdate();
        }
        when BEFORE_DELETE {
          this.beforeDelete();
        }
        when AFTER_INSERT {
          this.afterInsert();
        }
        when AFTER_UPDATE {
          this.afterUpdate();
        }
        when AFTER_DELETE {
          this.afterDelete();
        }
        when AFTER_UNDELETE {
          this.afterUndelete();
        }
      }
    }

If you checkout the TriggerHandler class you’ll see that method within it. Basically what it’s doing is checking whether or not the trigger should run, and then running the method for the respective trigger context we’re currently in. Pretty simple, pretty kewl. That’s literally all it takes to leverage this framework for your own code.

More info on the trigger framework

There is a lot more stuff you can do with this framework and you can check out more of it in the video linked above or in the github repo. One of the best features is being able to dynamically turn your triggers on and off super easily from the code! So make sure to check that out!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Similar Posts