You need to sign in to do that
Don't have an account?
pradeep kumar yadav
Do we always make static methods in Trigger, or is there any special requirement where we have to write non-static methods?
Is there any special case where we have to go with non-static methods in Trigger?
,
If the class contains a static method, it can directly call by its Class Name and its related Member Name and
If the Class contains Non - static member than it gets called by after instantiating the class, then while
using instantiation name and Non-static member Name, you can make calls to that member.
For example --
Hope it might help you.
Thanks
Akshay
I know how to use static and non-static methods, but my question is different.
My question is related to Trigger not static or non-static.
My question is that:- "Is there any requirement in the trigger that we cannot use static methods and we have to go for non-static methods"
if you use static than no need to instance object and called direct.
for static method :
Trigger:
trigger AccountTrigger on Account (before insert){
if ((Trigger.isBefore) && (Trigger.isInsert)){
AccountTriggerHandler.CreateAccounts(Trigger.New);
}
}
Apex class :
public class AccountTriggerHandler{
public static void CreateAccounts(List<Account> acc){
List<Account> accts = new List<Account>();
for(Account a:acc){
if(a.BillingState!=a.ShippingState){
a.BillingState.addError('BillingState must be same as ShippingState');
}
}
}
}
for Non static method :
Trigger:
trigger AccountTrigger on Account (before insert){
if ((Trigger.isBefore) && (Trigger.isInsert)){
AccountTriggerHandler obj = new AccountTriggerHandler(); // Here We need to first create Object than call apex claxx
obj.CreateAccounts(Trigger.New);
}
}
Apex class :
public class AccountTriggerHandler{
public void CreateAccounts(List<Account> acc){
List<Account> accts = new List<Account>();
for(Account a:acc){
if(a.BillingState!=a.ShippingState){
a.BillingState.addError('BillingState must be same as ShippingState');
}
}
}
}
if you found this answer helpful then please mark it as best answer so it can help others.
Thanks
Akshay
You are talking about features of static and non-static methods.
but I am talking about Trigger (When to go for a static approach and non-static approach in the context of the trigger).
Thanks for your reply, but my question is different.
If we see it in a general way, when to go static and when to go by instance, this article helps:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm
This is how I understand it(from my java background, however in java, class is shared across the application and not just the short lived transaction as in apex):
if you would want to work with variables or methods whose only state can be shared accross an entire apex transaction, then go static.
e.g.,
Utility methods
or variable which retain a common value at class level for entire apex tx
Otherwise, if there is a need to maintain mutiple instances of a class to hold different variable, then go non static or instance.
e.g.,
List of 10 people(instances of class People), with own property values for name, age etc
non static method : people1.giveMoney(money) -> you want to give money to a specific instance of people class
Let know if it made any sense.
Greetings!
You can not define any method in the test class.
You can call static and nonstatic methods in a trigger.
If you want to call a nonstatic method in the trigger.
Use like- If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi