Skip to main content

If you are reading this blog, most probably you had googled this keyword “Salesforce trigger best practices” and the result from that was to use Salesforce trigger framework and most of the blogger will either build their own framework or recommends “Kevin’o hara” framework.

Today I will be sharing which type of Salesforce trigger approach suit your organization, projects and your team.
Below are some of the best practices & recommendation for building triggers/apex:

  • One Trigger Per Object
  • Bulkify your Code
  • Avoid SOQL Queries or DML statements inside FOR Loops
  • Using Collections, Streamlining Queries, and Efficient For Loops
  • Querying Large Data Sets
  • Use @future Appropriately
  • Avoid Hardcoding IDs

Trigger Handler Framework & “Kevin’o hara” framework.

Trigger handle framework mainly build base on this four concept:

aodigy-image 6

Trigger → Initial the logic whenever a DML occur

Interface → Consolidated for beforeInsert,beforeUpdate,beforeDelete,afterInsert,afterUpdate,afterDelete,afterUndelete, so that this can be reusable across all triggers.

Handler → To handle complex condition logic and which method to be run for each context.

Helper → Reusable method or function that can be supported by handle class.

More information can be retrieve from: https://www.salesforceben.com/the-salesforce-trigger-handler-framework/

Pro & Con for Trigger Handler Framework

Pro:

  • Most popular framework around Salesforce ecosystem
  • Scalable
  • Keep the code clean
  • Helps the developer to code within the best practices
  • Methods can be reuse

Cons:

  • Have to build extra feature such as trigger.bypass to your own framework unless is Kevin’o hana framework
  • Don’t have full control on which method to run in an orderly manner
  • Don’t have control on auto-launch flow execution
  • You have to define your own test classes for the framework

Conclusion:
This is a good framework but requires experience developer with substantial object-oriental knowledge and apex experience to understand the framework. Good for medium to huge size project to use this framework for trigger development.

Trigger Action Framework

Introduce on 2021, by Mitch Spano, this framework give the flexibility to both developer and administrator to manage trigger and flow execution orders.

This are the four concept uses by Trigger Action Framework:

aodigy-image 7

Trigger → Initial the logic whenever a DML occur

Metadata Trigger Handler → This is a unified handler class to be call for all triggers and it control the custom meta object for both Trigger Action mdt and sObject trigger setting mdt.

Trigger Action → Interface class to be use to define the type of actions:
– beforeInsert
– beforeUpdate
– beforeDelete
– afterInsert
– afterUpdate
– afterDelete
– afterUndelete

Flow Trigger Record → abstract class to be use as extends class for sObject. This is to enable the trigger to invoke a auto-launched flow during the trigger execution context. Running sequence of the auto-launched flow are control by trigger action metadata.

More information can be retrieve from: https://github.com/mitchspano/apex-trigger-actions-framework

Pro & Con for Trigger Action Framework

Pro:

  • Plenty of ready features (i.e trigger.bypass, recursion prevention, support flows)
  • Scalable
  • Keep the code clean
  • Helps the developer to code within the best practices
  • Trigger action class can be reuse
  • Actions can be deploy independently (SFDX friendly)

Cons:

  • New framework lower adoption compared to standard trigger handler framework.
  • High overhead as each actions need to create one apex class.
  • High overhead as developer or admin need to configure metadata each time a new action is introduce.
  • The learning crave is not suitable for beginner admin or developer.

Conclusion:
Suitable for greenfield projects that are medium to large scale. I believe trigger action framework will be the next popular trigger framework in Salesforce ecosystem especially this blend well with SFDX methodology. Having say that, if your Salesforce org already have an existing framework or logic it is not advisable to introduce trigger action framework to your current practice as this will bring in more complexity. But if your organization plan to move to SFDX, then migrating all your triggers to trigger action framework will be a great investment for long term strategic.

References:

https://www.salesforceben.com/the-salesforce-trigger-handler-framework/
https://www.asagarwal.com/recommended-framework-for-salesforce-triggers/
https://www.apexhours.com/trigger-actions-framework/
https://trailhead.salesforce.com/en/content/learn/modules/success-cloud-coding-conventions/implement-frameworks-sc
https://github.com/danappleman/Triggers-without-frameworks
https://github.com/kevinohara80/sfdc-trigger-framework
https://github.com/mitchspano/apex-trigger-actions-framework
https://cloudsundial.com/the-third-age-of-trigger-frameworks
https://cloudsundial.com/apex-trigger-actions-and-nebula-triggers

Some of our clients

clientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclientclient