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
VKrishVKrish 

How to exit from a trigger without Throwing an Error

Hi All,

 

Here is my situation:

I have custom object with a trigger that updates a field in Contact. This trigger fires in insert, update & delete.

I also have a Contact trigger which updates this custom object (insert/delete) on certain conditions alone. The contact trigger also handles many other functionality, but each is executed only on certain condition.

The contact and custom object have master-child relationship. 1 contact can have many child custom objects.

 

On a particular condition, when a contact trigger insert or delete one of the child custom record, the recursion occurs which I want to stop.

To do this, I created a custom text field (source__c) in the custom object (custom__c) and updates the value of it (Ex: 'From Contact Trigger') in Contact trigger alone.

Now I want to exit from the Custom_Trigger without executing the rest of code when the custom__c.source__c == 'From Contact Trigger'

The break or continue statement wont work here. There is no exit() stmt. I also don't want to explicitly through error to the user since user.

 

I have developed a scheduled apex that does the same job as custom object trigger and run it once a day so the field value in contact updated.

So updating the contact field is taken care. But I want to avoid the exception which is preventing the custom record insert/delete during contact trigger.

 

Is there an elegant way to do this? Is there any method available in apex that substitute exit()?

 

Please help!

Thanks in advance!

Leon MorockiLeon Morocki

You can use a static variable within a class to save the state. You will be able to access the value set in the first trigger from the context of the second trigger.

VKrishVKrish

Thanks for the reply!

I store the value in a field. But a static varibale in class should also do.

But my question is when I refer this value in 2nd trigger, how do I exit the trigger without executing rest of the code something like using exit() method.

Leon MorockiLeon Morocki

Well, i think there is no explicit way to return from a trigger, but why don't you just include the code you don't want to execute in a conditional block like:

 

trigger .... {
    if (...) {
        // do stuff
    }
}

 

VKrishVKrish

Thanks!

I wanted to avoid this because the trigger handles many jobs at many situations, and the code that goes inside this if condition is big.

And I may have to add this check in different places due to business complexity.

That's why I started looking for some way to use exit() so I don't have to look repeat this check in different places.

But if there is no other go, I think I am left with no choice :(

 

Thanks again

Leon MorockiLeon Morocki

But of course you can delegate the whole processing to a class method and handle it normally like:

 

trigger ... {
    Processor.process();
}

-------------

public class Processor {

    public static void process() {
        if (...) return;
// do stuff } }

 

VKrishVKrish

Thanks!

Ofcorse! I do call 3 different classes for different functionlities(certain updates on international compliance clearance, end user interest, their actual eligibility etc) depending upon 3 situations on which trigger fires.

Adil Aleem 17Adil Aleem 17
Use return;