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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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










share|improve this question







New contributor




Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    1
    down vote

    favorite












    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










    share|improve this question







    New contributor




    Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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










      share|improve this question







      New contributor




      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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







      apex trigger






      share|improve this question







      New contributor




      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Adam B

      62




      62




      New contributor




      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Adam B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote













          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.






          share|improve this answer



























            up vote
            1
            down vote














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




            Because it’s not allowed syntactically. In any programming language, you have to adhere to the syntax as defined by the platform. Similar is the case for any Apex Trigger. The trigger syntax as defined on its documentation
            is as below, and so you have to make sure the way you write your trigger follows that, else you end up with compilation errors.



            trigger TriggerName on ObjectName (trigger_events) 
               code_block



            It is not necessarily required that you have to write a class and that you call it from your trigger. Why we do it that way is to have a modular approach and separate and contain much of “boilerplate” code outside of trigger. If you have say few line of statements be executed, you can choose to write that directly within the trigger without the need of any other class.






            share|improve this answer



























              up vote
              1
              down vote













              Good question! And, yes, you're right on target.



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



              More info here:



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






              share|improve this answer





























                up vote
                0
                down vote













                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 q233513 on Account (before insert) 
                void helper()
                System.debug('Helper in trigger.');

                class q222513
                void helper()
                System.debug('Helper in inner class.');


                helper();
                q222513 help = new q222513();
                help.helper();





                share




















                  Your Answer







                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "459"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );






                  Adam B is a new contributor. Be nice, and check out our Code of Conduct.









                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233513%2fwhy-cant-i-add-a-helper-method-in-a-trigger%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote













                  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.






                  share|improve this answer
























                    up vote
                    1
                    down vote













                    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.






                    share|improve this answer






















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      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.






                      share|improve this answer












                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 hours ago









                      Michal Vavra

                      470111




                      470111






















                          up vote
                          1
                          down vote














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




                          Because it’s not allowed syntactically. In any programming language, you have to adhere to the syntax as defined by the platform. Similar is the case for any Apex Trigger. The trigger syntax as defined on its documentation
                          is as below, and so you have to make sure the way you write your trigger follows that, else you end up with compilation errors.



                          trigger TriggerName on ObjectName (trigger_events) 
                             code_block



                          It is not necessarily required that you have to write a class and that you call it from your trigger. Why we do it that way is to have a modular approach and separate and contain much of “boilerplate” code outside of trigger. If you have say few line of statements be executed, you can choose to write that directly within the trigger without the need of any other class.






                          share|improve this answer
























                            up vote
                            1
                            down vote














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




                            Because it’s not allowed syntactically. In any programming language, you have to adhere to the syntax as defined by the platform. Similar is the case for any Apex Trigger. The trigger syntax as defined on its documentation
                            is as below, and so you have to make sure the way you write your trigger follows that, else you end up with compilation errors.



                            trigger TriggerName on ObjectName (trigger_events) 
                               code_block



                            It is not necessarily required that you have to write a class and that you call it from your trigger. Why we do it that way is to have a modular approach and separate and contain much of “boilerplate” code outside of trigger. If you have say few line of statements be executed, you can choose to write that directly within the trigger without the need of any other class.






                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote










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




                              Because it’s not allowed syntactically. In any programming language, you have to adhere to the syntax as defined by the platform. Similar is the case for any Apex Trigger. The trigger syntax as defined on its documentation
                              is as below, and so you have to make sure the way you write your trigger follows that, else you end up with compilation errors.



                              trigger TriggerName on ObjectName (trigger_events) 
                                 code_block



                              It is not necessarily required that you have to write a class and that you call it from your trigger. Why we do it that way is to have a modular approach and separate and contain much of “boilerplate” code outside of trigger. If you have say few line of statements be executed, you can choose to write that directly within the trigger without the need of any other class.






                              share|improve this answer













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




                              Because it’s not allowed syntactically. In any programming language, you have to adhere to the syntax as defined by the platform. Similar is the case for any Apex Trigger. The trigger syntax as defined on its documentation
                              is as below, and so you have to make sure the way you write your trigger follows that, else you end up with compilation errors.



                              trigger TriggerName on ObjectName (trigger_events) 
                                 code_block



                              It is not necessarily required that you have to write a class and that you call it from your trigger. Why we do it that way is to have a modular approach and separate and contain much of “boilerplate” code outside of trigger. If you have say few line of statements be executed, you can choose to write that directly within the trigger without the need of any other class.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 2 hours ago









                              Jayant Das

                              6,6122320




                              6,6122320




















                                  up vote
                                  1
                                  down vote













                                  Good question! And, yes, you're right on target.



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



                                  More info here:



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






                                  share|improve this answer


























                                    up vote
                                    1
                                    down vote













                                    Good question! And, yes, you're right on target.



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



                                    More info here:



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






                                    share|improve this answer
























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      Good question! And, yes, you're right on target.



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



                                      More info here:



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






                                      share|improve this answer














                                      Good question! And, yes, you're right on target.



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



                                      More info here:



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







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 2 hours ago

























                                      answered 2 hours ago









                                      Shane Steinfeld

                                      832415




                                      832415




















                                          up vote
                                          0
                                          down vote













                                          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 q233513 on Account (before insert) 
                                          void helper()
                                          System.debug('Helper in trigger.');

                                          class q222513
                                          void helper()
                                          System.debug('Helper in inner class.');


                                          helper();
                                          q222513 help = new q222513();
                                          help.helper();





                                          share
























                                            up vote
                                            0
                                            down vote













                                            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 q233513 on Account (before insert) 
                                            void helper()
                                            System.debug('Helper in trigger.');

                                            class q222513
                                            void helper()
                                            System.debug('Helper in inner class.');


                                            helper();
                                            q222513 help = new q222513();
                                            help.helper();





                                            share






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              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 q233513 on Account (before insert) 
                                              void helper()
                                              System.debug('Helper in trigger.');

                                              class q222513
                                              void helper()
                                              System.debug('Helper in inner class.');


                                              helper();
                                              q222513 help = new q222513();
                                              help.helper();





                                              share












                                              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 q233513 on Account (before insert) 
                                              void helper()
                                              System.debug('Helper in trigger.');

                                              class q222513
                                              void helper()
                                              System.debug('Helper in inner class.');


                                              helper();
                                              q222513 help = new q222513();
                                              help.helper();






                                              share











                                              share


                                              share










                                              answered 24 secs ago









                                              sfdcfox

                                              229k10176390




                                              229k10176390




















                                                  Adam B is a new contributor. Be nice, and check out our Code of Conduct.









                                                   

                                                  draft saved


                                                  draft discarded


















                                                  Adam B is a new contributor. Be nice, and check out our Code of Conduct.












                                                  Adam B is a new contributor. Be nice, and check out our Code of Conduct.











                                                  Adam B is a new contributor. Be nice, and check out our Code of Conduct.













                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233513%2fwhy-cant-i-add-a-helper-method-in-a-trigger%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                  What does second last employer means? [closed]

                                                  One-line joke