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
ThathulaThathula 

How to terminate a running trigger based on a condition.

Hi, I've a trigger and a service class.
I need to check for a condition in service class and if that matches i need to terminate that event(terminate), not that just function but whole process

How can i do that?

Trigger--> Service Class Function 



Thanks
 
Abhishek_DEOAbhishek_DEO
You can use addError() method to terminate the transaction. However, you should add this method on only those record that are part of transaction. If you add this method addError on some other record which is not a part of rigger context but you retrieve it using SOQL, it may give you error

for eg.
public string myName(list<custom__c> myrec) // suppose you are calling this method from trigger(using class)
{
  myrec[0].adderrr('your error message') // will work
}

Please acced this link  (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm) for more information
Tarun J.Tarun J.
Hello Thathula,

You can use return for this. In your service class function, you can set some static varaible as true and check it in trigger in the next line where you are calling the service class method. 
//Code in service class method
if(<Your Condition>){
skipTrigger = true;
}

//Code in trigger after calling service class method
if(skipTrigger == true){
return;
}

-Thanks,
TK


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
ThathulaThathula
Hi everyone,

Thanks for your reply. However what i need is not to give an error message, but to terminate whole trigger, without giving any output.
In C we have system.exit(0). Dont we have something like that in APEX?

Thank you
Tarun J.Tarun J.
Have you tried above code? It will not throw any trigger. Just bypass from executing any other part of code in trigger.
 
It would be good if you share your code also. This will help to better understand the issue.
ThathulaThathula
Thank you Tarun for your kind reply. Actually return; will bypass the current function right? not the whole exection. I need to bypass whole execution. code will be something like this

myTrigger{

myServiceClass.method(with map and list)
myServiceClass.method2();//=====> if i use return this will be called, i need to terminate from whole process

}

myServiceClass{

public static method(parameters) {

if(someCondition == true){

return;// ===> After i terminate method2 also should not be called 

}

}


}


Thankssssssss






 
David ZhuDavid Zhu
It seems Tarun's solution would work. if you twick your code a little bit
 
myTrigger{

    myServiceClass.method(with map and list)
    if (someCondition != true)
        myServiceClass.method2();
    }

    myServiceClass{

    public static method(parameters) {

    if(someCondition == true){

    return;// ===> After i terminate method2 also should not be called 

    }

}


}

 
ThathulaThathula
Actually i can not touch my trigger codes now. I need to find some solution without changing myTrigger. Just by only chaning method !!
 
Tarun J.Tarun J.
In that case, trigger will call second method everytime it get executed. In order to avoid the execution of functionality in method2(), you can put IF block in the starting of method and check the condition.
 
public static void method2(){

if(skipTrigger != true){

//Code which you want to skip if it matches the condition in method1().

}

}

-Thanks,
TK


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.