Trigger Framework in Salesforce

Trigger Framework in Salesforce

Learn how to completely and cleanly control your Apex code with Trigger Frameworks, why they’re useful, and the various ways to implement them. In this session will talk about Apex Trigger Framework in Salesforce and benefit of using a trigger framework. How many trigger framework are available in Salesforce, which one is lightweight apex trigger framework and Comparison of different approaches.

In this session we talk about all below Trigger Framework

  1. Trigger Handler Pattern
  2. Trigger Framework using a Virtual Class
  3. Trigger Framework using an Interface
  4. An architecture framework to handle triggers

Why Trigger Framework

Triggers are a powerful tool that can do great things when used correctly but cause a lot of headache when used incorrectly. Triggers without structure can be messy. They can interfere with one another and cause huge performance and debugging problems.

  • Generic code that can be extended for any object
  • Ensures triggers are consistently handled and only required code is needed.
  • Allows for simple Triggers and handlers.
  • handles routing for you
  • enforce consistent trigger behavior
  • Easily allows for trigger bypasses
  • Allow you to enforce different behavior for triggers that start a process vs. mid process.

Trigger Handler Pattern

Please check this post to learn about Handler pattern and code. Lets talk about what is the advantage of Trigger Handler Patter.

  1. Apex Class that handles trigger logic
  2. Allows code to be called from other code or tests
  3. Uses specific Trigger contexts and Trigger variables for routing
  4. Keep Triggers Simple
  5. Allow for greater flexibility
  6. Make code reusable
  7. Unit tests are much easier
Trigger Handler Pattern

What is missing in Handler pattern ?

  1. Still duplicated code in every trigger
  2. How can we simplify things?
  3. How can we make our process repeatable?

Trigger Framework using a Virtual Class

Please check this post to learn more about Virtual Class Trigger Framework. 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

Trigger Framework using a Virtual Class

Accomplishments with this Trigger framework :-

  •  1 line trigger
  • Only need to add handler methods that we want to use
  • All routing is handled for us

Trigger Framework using an Interface

Please check this post to learn about this framework.

Trigger Framework using an Interface

Accomplishments with this Trigger framework :

  • 1 line trigger
  • All routing is handled for us
  • All handlers are consistent and will have the same methods
  • Multiple ways to deactivate a trigger

An architecture framework to handle triggers

Please check this post to learn about this framework.

An architecture framework to handle triggers

Accomplishments with this Trigger framework :-

  1. One line trigger
  2. All routing is handled for us
  3. Establish all methods while letting us pick and choose which ones we want
  4. Individual event handler methods

Recording

Agenda:

  1. How Triggers Work
  2. Why Trigger Handlers
  3. Why Trigger Frameworks
  4. Create a Trigger Framework using a Virtual Class
  5. Create a Trigger Framework using an Interface
  6. Comparison of different approaches
  7. Drawbacks to Frameworks
  8. Summary and Wrap Up

Trigger Actions Framework

Administrators use platform products like Flow to deliver powerful business processes, and developers can write Apex to handle more complicated scenarios. The biggest question for Salesforce architects has been how to arrange these tools together to build complete solutions. Check this post to learn about Trigger Action framework in Salesforce.

Further Learning

Here is link for all frameworks:-

If you are new to Salesforce then check our FREE Developer training. Please subscribe our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

Summary

There are multiple trigger frameworks are available in Salesforce. All of them are good and have some advantages and disadvantages. You can select any Salesforce Trigger Framework base on your organization size and client need. Dont forget to let us know which Salesforce Trigger Framework you like and using in your org.

Amit Chaudhary

Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates. He is a active blogger and founder of Apex Hours.

Share this article

2 Comments

  • Please send me the notifications of new posts

  • Hi All,

    Firs of all, thank you so much for this blog, it’s really helpfull to me.

    I’d like to view the code related with each framework. But, the link in the “Trigger framework using an interface” section send me to http://chrisaldridge.com/triggers/lightweight-apex-trigger-framework/. However, that page give the following error:
    Forbidden
    You don’t have permission to access /triggers/lightweight-apex-trigger-framework/ on this server.

    Could you correct this please so I can access to the code?

    Thanks in advance.

    Regards,

    Juan.

Leave a reply

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,545 other subscribers

Our Supporter

RECENT POSTS

Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!