Development questions
How do you make an asynchronous callout from a trigger (@future(callout=true) vs Queueable)?
@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
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?