How do you make an asynchronous callout from a trigger (@future(callout=true) vs Queueable)?
Answer
@future(callout=true): static void method, only primitive/collection-of-primitive parameters (pass Ids and re-query), no chaining, cannot be called from future/batch, no job Id returned.
Queueable + Database.AllowsCallouts: accepts SObjects/complex types, returns a job Id, supports chaining, can be monitored.
apex
public class CalloutJob implements Queueable, Database.AllowsCallouts {
Set<Id> ids;
public CalloutJob(Set<Id> ids) { this.ids = ids; }
public void execute(QueueableContext ctx) {
// query by ids, make HTTP callout, update results
}
}
// in trigger handler
System.enqueueJob(new CalloutJob(accountIds));
Prefer Queueable for new work.
apex-triggertriggercalloutasync