Why can't I add a helper method in a trigger?
Clash 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
apex trigger
New contributor
add a comment |Â
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
apex trigger
New contributor
add a comment |Â
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
apex trigger
New contributor
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
apex trigger
New contributor
New contributor
New contributor
asked 3 hours ago
Adam B
62
62
New contributor
New contributor
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
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();
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 2 hours ago
Michal Vavra
470111
470111
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 2 hours ago
Jayant Das
6,6122320
6,6122320
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited 2 hours ago
answered 2 hours ago
Shane Steinfeld
832415
832415
add a comment |Â
add a comment |Â
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();
add a comment |Â
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();
add a comment |Â
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();
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();
answered 24 secs ago
sfdcfox
229k10176390
229k10176390
add a comment |Â
add a comment |Â
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.
Adam B is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password