Activity Forums Salesforce® Discussions How to use the Helper class in Salesforce Apex Trigger? 

  • krati

    Member
    January 28, 2020 at 1:29 pm

    Hi Arun,

    You can Use Helper Class in Trigger. Your Helper Class is an Apex Class only, by using inside the helper class create a method which can be used by different trigger or different trigger event. You just need to pass the your trigger. new or trigger.old value in the method parameter.

    For Example, here same method is getting called for different trigger events.

    if(trigger.isInsert || trigger.isUpdate){
    
       HelperClass.accountTotalAmountAfterInsertOrUpdateOrDelete(trigger.new);
    }
    if(trigger.isDelete){
       HelperClass.accountTotalAmountAfterInsertOrUpdateOrDelete(trigger.old);
    }
    
    // Helper Class
     public static void updatePrimaryContactAfterDelete(List<Contact> listOfContacts){
      // your code here
    }

     

  • Dharmendra

    Member
    September 23, 2021 at 9:57 am

    How to use Static Helper Classes in Exception Handling?

  • Emilyn

    Member
    January 13, 2022 at 10:51 pm

    Hi Arun!
    It's important that we use handlers for our triggers for many reasons:

    • they keep logic out of the triggers themselves, which significantly improves legibility and ease of alteration because it allows the trigger itself to act as a sort of router, passing along tasks to the handlers
    • Because the logic is separated out this way, it is far easier to pinpoint bugs and such when they occur
    • they ensure that we are keeping our overall test coverage high for deployment, requiring a 75% coverage minimum where a trigger only requires a 1% min coverage

    I'm sure there are many more, but these are the main ones I could think of off the top of my head.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos