4

Declaring a static boolean variable doesn't work on the queueable interface, as it is running asynchronously. My flow:

Trigger:   --> After Update
Queueable: --> Callout external system
Update:    --> Update Same Object (this leads to step 1)

1 Answer 1

10

You can use exactly the same safe Boolean trigger guard you'd use within a normal trigger handler that recursively updates its object. Note that this is distinct from a simple static Boolean that requires a trigger to run only once per transaction: that pattern is broken.

It would look something like this:

class MyTriggerHandler {
    public static Boolean inRecursiveUpdate = false;

    public static void doUpdate(Map<Id, sObject> triggerNew) {
        // Or whatever trigger framework you use.
        if (inRecursiveUpdate) return;

        // Do stuff.
        System.enqueueJob(new MyQueueable());
    }
}

class MyQueueable implements Queueable {

    public void execute(QueueableContext qc) {
        // Prepare updates, then:
        MyTriggerHandler.inRecursiveUpdate = true;
        update mySobjects;
        MyTriggerHandler.inRecursiveUpdate = false;
    }
}

The suppression flag allows you to very selectively instruct the trigger not to run for specific updates you make in your Queueable job to avoid recursion, while not otherwise impacting trigger functionality.

2
  • It worked! but what was the reason you again set it false after the update ?
    – Ragul
    May 2, 2020 at 23:31
  • 3
    Because you want the trigger to continue operating on any further DML operations in the course of the transaction. You only want to suppress operation during the course a a single recursive update.
    – David Reed
    May 2, 2020 at 23:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .