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
David_David_ 

Static vs. Non-Static: Best Practice for Trigger Handlers and Classes

Hey there,

when writing Trigger Handler Classes and calling the respective methods from a Master Trigger there are different ways to achieve this. The keywords are Static and Non-Static .

In other words:
public void
vs.
public static void
I was wondering what's the best practice to write and call a method from a Master Trigger. In fact both options will work - but I once read that going for non-static was always the better choice. Why is that?

Given we have a method in our class that creates Assets from an Opportunity's line items (Opportunity Products). 

Static - Master Trigger call:
OpportunityTriggerHandler.createAssets(Trigger.new)
Question 1:

I saw this approach quite often in forums and blog articles - is this approach generally recommended and reasonable?

Non-Static - Master Trigger call:
OpportunityTriggerHandler oppHandler = new OpportunityTriggerHandler();
oppHandler.createAssets(Trigger.new);
Question 2

Can I consider non-static as best practice and if yes, why?

Looking forward to your feedback!
 
Deepali KulshresthaDeepali Kulshrestha
Hi David_,

Greetings to you!

I have researched about Static vs. Non-Static: Best Practice for Trigger Handlers and Classes.There is no role of static and non- static in the trigger best practice.
Please follow below link for more information about trigger best practice.
https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Dushyant srivastava 8Dushyant srivastava 8
Hi David,

Method declaration as static or non static is nothing to do with Apex Trigger Best Practice.

Difference is that Static method can acces static variable and belongs to the class whereas Non Static method and variables can be accesed only the object of the class.

Second Major difference is that Static variable can not be overridden.

Thanks & Regards,
Dushyant Srivastava