Why can't I add a helper method in a trigger?

I am guessing it's because a trigger is meant to point to a class which can hold methods and be object oriented

up vote 7 down vote accepted

You can add helper methods to triggers. You can even add classes to your triggers. It may seem odd, but the code definitely compiles and runs the way you expect it to. The modern recommendation is to use helper classes, but nothing prevents you from writing helper methods in a trigger. Please note that helper methods written this way cannot be used outside of the trigger it is defined in (hence, the reason why it's recommended you don't do this), but for people who don't want the hassle of a trigger framework, defining helper methods this way can lend legibility to your trigger.


trigger AccountTrigger on Account (before insert) {
    void helper() {
        System.debug('Helper in trigger.');
    }
    with sharing class HelperClass {
        void helper() {
            System.debug('Helper in inner class.');
        }
    }
    helper();
    HelperClass help = new HelperClass();
    help.helper();
}
  • 1
    Thank you all! This is my first question here and I’ve been blown away by the speed and depth of the responses :) – Adam B Sep 22 at 19:31
  • I didn’t even know that you can actually do it this way. Thanks for sharing. In fact I had thought that it won’t even compile at first place (tbh, hadn’t tried at all) and thus thought it was not even syntactically allowed. – Jayant Das Sep 23 at 1:12
  • @JayantDas There's a lot of really weird syntax things you can do in Apex that are not particularly in the documentation. Of course, best advice is to stick with what's in the documentation, but Apex is a lot weirder than the documentation lets on. – sfdcfox Sep 23 at 2:23
  • So True. And I think its better to stick to good “old school of thought” utilizing the OOP concepts. And in true implementation, it’s always better to keep things simple and more readable as most of the time will go in maintenance and utilizing any such syntax which majority of people will not be able to understand will just complicate things than solving anything. – Jayant Das Sep 23 at 2:27
  • Hey @sfdcfox -- could you change the class name in your example to something more easily distinguishable from the trigger name? For a minute there, I thought you were instantiating the trigger! Thanks. – Shane Steinfeld Sep 23 at 11:21

Triggers are not a real Apex classes and yes, they are meant to point to classes because of security concerns.

By default Saleforce executes code in system context. Object permissions, field-level security, and sharing rules aren’t applied. The default value is without sharing and in triggers you can't specify with sharing keyword.

Triggers itslef should not contain any business logic due to that.

  • 1
    Yes, you can put with sharing and without sharing classes inside your trigger if you wanted to. – sfdcfox Sep 22 at 19:27

A trigger in Salesforce is like a database "stored procedure". There isn't another analogy to be made with an object-oriented programming concept that I know of.

More info here:

https://en.wikipedia.org/wiki/Stored_procedure

That said, you can use methods inside triggers!

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.