Sub topic 10
Avoiding re-entry and job loops
This chapter assumes you can write a bulk-safe trigger and have read the earlier chapters on Queueable callouts. Here you will stop a callout response from re-triggering the work that produced it.
How the loop forms
A common pattern is to send accounts to an external system and write the result back, for example a Last_Synced__c field. That write is a DML update. It fires the trigger again, the trigger enqueues another job, the job writes back again, and the cycle repeats. Each pass consumes async Apex executions and callouts until a limit stops it.
Why a static flag is not enough
A common fix for trigger recursion is a static Boolean such as hasRun. Static variables last only for one transaction. A Queueable job runs in its own transaction, so its statics start fresh and the flag is reset. A static flag still helps against repeated trigger runs inside a single transaction, but it cannot stop a loop that passes through an async job.
Guards that work across transactions
| Guard | How it works | Trade-off |
|---|---|---|
| Change detection | Enqueue only if a field you send to the external system changed | Most precise. Needs `Trigger.oldMap`. |
| Context check | Skip when `System.isQueueable()`, `System.isFuture()` or `System.isBatch()` is true | Simple, but it also skips legitimate changes made by other async jobs. |
| Status field | Skip records already marked as syncing or synced | Needs an extra field and careful resetting. |
Change detection is usually the best choice. The write-back touches only fields the callout does not send, so the trigger sees no relevant change.
A guarded trigger
trigger AccountSyncTrigger on Account (after insert, after update) {
if (System.isFuture() || System.isBatch() || System.isQueueable()) {
return;
}
Set<Id> toSync = new Set<Id>();
for (Account a : Trigger.new) {
if (Trigger.isInsert) {
toSync.add(a.Id);
} else if (a.Name != Trigger.oldMap.get(a.Id).Name) {
toSync.add(a.Id);
}
}
if (!toSync.isEmpty()) {
System.enqueueJob(new AccountSyncJob(toSync));
}
}The trigger enqueues one job for the whole batch and only for records where Name, the field sent in the payload, is new or changed. The context check is a second layer of protection.
The write-back in the job
After a successful callout, update only a field that is not part of the change check. Last_Synced__c here is a custom DateTime field you would add to Account.
List<Account> updates = new List<Account>();
for (Account a : accounts) {
updates.add(new Account(Id = a.Id, Last_Synced__c = System.now()));
}
update updates;The update still fires the trigger. The guard finds that Name did not change and the context is Queueable, so nothing is enqueued.
Related limits
- A trigger transaction can enqueue up to 50 Queueable jobs. One job per batch of records keeps you far below that.
- A running Queueable can enqueue only one child job. Chaining jobs without an exit condition creates a loop of its own.
- A future method cannot be called from another future or Batch method. Calling it from the trigger while already in a future context throws an exception, so the context check above also protects
@futurecode.
Exercise
A trigger uses private static Boolean hasRun = false; to stop re-entry. Its Queueable job updates the same account after a callout, and the trigger still enqueues a new job each time. Why does the flag fail, and what is a better guard?
Show answer
The Queueable runs in a new transaction, so the static variable is reset to false and the trigger runs its logic again. A better guard compares Trigger.newMap with Trigger.oldMap and enqueues only when a field sent in the callout has changed. The write-back should touch only fields outside that comparison. Adding a System.isQueueable() check is a second layer of protection.