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)
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityYou 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.