function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
subaasubaa 

Can we add method block inside the Trigger file?

Hi,

 

Can we add method block inside the Trigger file? We got a scenario of executing the common block of statements for two set of records, which run serially. Can anyone assist me?

 

Regards,

SuBaa

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

No, you can't have methods in a trigger.  The way you would handle this is to create an static class that contains a static method with your code.  Then you can simply execute that from the trigger.

All Answers

bob_buzzardbob_buzzard

No, you can't have methods in a trigger.  The way you would handle this is to create an static class that contains a static method with your code.  Then you can simply execute that from the trigger.

This was selected as the best answer
Cory CowgillCory Cowgill

Just to elaborate on Bob_Buzzards point, best practice is to use a "Trigger Handler Pattern" where you encapsulate all your business logic in a static apex class. The only logic in your trigger file would be checking the Trigger context (Trigger.isInsert, Trigger.isBefore, etc) and then calling the proper static method on the handler class.

 

This is best practice for several reasons.

 

1. Triggers run in a system context, meaning if you have sharing rules and do queries in the trigger file than you will retrieve all records, bypassing security since System can see all the records. By using a Trigger Handler Class leveraing the "with sharing" keywords you can ensure you are enforcing sharing rules.

 

2. Encapsulating your logic in the trigger handler lets you write easier to maintain code and keeps all your business logic in one place. This lets you create smaller reusable chunks of code in methods, etc.

 

Here is an excellent blog post with a code sample on how to do this:

http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx

subaasubaa

Hi Cory Cowgill,

 

Thanks for sharing the Best Practises about Trigger Handler Pattern. Does Salesforce support any other Patterns which can improve the apex (trigger) programming?

 

Regards,

SuBaa

bob_buzzardbob_buzzard

Its not so much that salesforce supports patterns, more a case of writing your code according to a particular pattern. Thus you should be able to use many patterns.

asadim2asadim2
Apparently this is now possible!! Not sure when this feature went public.
VeerSaaVeerSaa
Hi Subaa,

Officially Saleforce doesn't support any specific Trigger pattren but until and unless any pattern is being part of Object Oriented structure it works.
Below are some links for reference which has different patterns and last but nnot least Apex Design patterns you can find from Trailhead.

http://www.tgerm.com/2012/01/salesforce-apex-trigger-template.html
http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx

You can invent your pattern too if you can :)

Regards,
Virender Singh