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
Shree KShree K 

How to make a trigger run only when Static variable of a class is false?

Hi All,
I have an Apex class Class1 with method1,method2 and method3 in it, i would like to set a static variable V1=True initially and have below trigger to be run when the Static variable V1 is false.
trigger Trigger1 on Account (before insert, before update) {


    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
        Class1.Method1(Trigger.new);
        Class1.Method2(Trigger.new);
    }
    if((Trigger.isInsert || Trigger.isupdate) && Trigger.isAfter){
    
        Class1.Method3(Trigger.new,trigger.old);
    }  

}
Help will be appreiated,

Thanks
CvK
BenazirBenazir
Hi,

Please try the below code. If your static variable is true, then the trigger won't run.
trigger Trigger1 on Account (before insert, before update) {
    
    if(class1.V1)
        return;
    
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
        Class1.Method1(Trigger.new);
        Class1.Method2(Trigger.new);
    }
    if((Trigger.isInsert || Trigger.isupdate) && Trigger.isAfter){
        
        Class1.Method3(Trigger.new,trigger.old);
    }  
    
}

Thanks,
Benazir.