Activity › Forums › Salesforce® Discussions › How to use the Helper class in Salesforce Apex Trigger?
Tagged: Apex Class, Event, Salesforce Apex Trigger, Trigger Event
-
How to use the Helper class in Salesforce Apex Trigger?
Posted by Arun on January 28, 2020 at 1:19 PMHow to use the Helper class in Salesforce Apex Trigger?
Emilyn replied 4 years, 4 months ago 4 Members · 3 Replies -
3 Replies
-
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 } - [adinserter block='9']
-
How to use Static Helper Classes in Exception Handling?
-
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.