Activity Forums Salesforce® Discussions What are the advantages of Trigger Handler in comparison to the normal trigger operation in Salesforce?

  • chanchal kumar

    Member
    August 8, 2018 at 2:16 pm

    hello Anurag,

    Advantages of trigger Handler are:

    • facilitates logic-less triggers and One Trigger Per Object, which itself has many benefits(including fine grained control over order of execution)
    • improves readability and Separation of Concerns(this improved readability makes version control more fruitful as well)
    • optionally facilitates selective disablement
  • Parul

    Member
    September 18, 2018 at 7:29 am

    Hi

    It is always a best practice to write a single trigger on object and process all operation in apex class (we called it as handler) instead of writing logic in trigger. In handler you can specify the order of event based on different context variables. If you write many triggers in on object,
    then you cannot control the order in which triggers gets executed.

    For best practice, use below suggestion:

    1. Pass all trigger context variables to static method (say “OperationManager”)in handler class.
    2. Create different static methods for different operations which you need to perform.
    3. Control the order of different operation in “OperationManager” static method based on context variables.
    4. Also you can create different apex classes and call them from “OperationManager” static method.

     

    Thanks.

  • shariq

    Member
    September 21, 2018 at 12:30 am

    Hi,

    Creating handler will give developer a better understanding of the code, it is the best practice, and you can also use this class at multiple places.

    Hope this helps.

  • Parul

    Member
    September 21, 2018 at 4:43 am

    Adding more:

    Benefits of the Trigger Handler Pattern
    Related reading: Trigger Frameworks and Apex Trigger Best Practices

    While my preferred pattern differs slightly, the above article lays out many of the advantages. I personally find a handler implementation must-have because it:

    facilitates logic-less triggers and One Trigger Per Object, which itself has many benefits(including fine grained control over order of execution)
    improves readability and Separation of Concerns(this improved readability makes version control more fruitful as well)
    optionally facilitates selective disablement

     

    Thanks

Log In to reply.

Popular Salesforce Blogs