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
pradeep kumar yadavpradeep 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?
Raj VakatiRaj Vakati
Its not like always to use the static method .. but dependes on the contect .. 
 
public class P { 
   public static boolean firstRun = true; 
}
 
trigger T1 on Account (before delete, after delete, after undelete) { 
       if(Trigger.isBefore){
          if(Trigger.isDelete){
             if(p.firstRun){
                 Trigger.old[0].addError('Before Account Delete Error');
                  p.firstRun=false;
              } 
           }
        }
}

 
pradeep kumar yadavpradeep kumar yadav
Sorry, My question is different, I want real-time example.
Akshay_DhimanAkshay_Dhiman
Hi Pradeep
,
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 -- 
 
public Class ABC 
{
public static void showName()
{
System.debug('Name present in static member');
}
public void showName2()
{
System.debug('Name present in Non - static member');
}
}

In Execute anonymous  --
ABC.showName();  // It will get Executed Static member
ABC.showName2(); // It will Not get executed like this because Non - static member cannot get executed directly by class Name.

ABC ob = new ABC();
ob.showName2(); // Now non - static member gets executed.


Hope it might help you.

 Thanks 
 Akshay
pradeep kumar yadavpradeep kumar yadav
Hi 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"
Akshay_DhimanAkshay_Dhiman
@Pradeep, We can use both static or nonstatic in the trigger.

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
pradeep kumar yadavpradeep kumar yadav
@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.
ArvindSArvindS
@Pradeep,
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.


 
Suraj Tripathi 47Suraj Tripathi 47
Hi Pradeep,
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-
ClassName clsObj = new ClassName();
clsObj.methodName();
If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi