AfterUpdate.Dispatcher
Make one bulk call per chunk with the records that qualify, such as enqueueing a Queueable or publishing events.
Signature
apex
public interface Dispatcher extends AfterUpdate.Handler {
Boolean dispatchOnAfterUpdateWhen(TriggerTypes.UpdateRecord record);
void dispatchOnAfterUpdate(TriggerTypes.UpdateRecords records);
}dispatchOnAfterUpdateWhen(record): called once per record in the chunk; with a RecursionGuard, a record past its limit is skipped without a call. Returntrueto include the record in the dispatch.dispatchOnAfterUpdate(records): called once, after every record was checked, with the qualified records; not called when none qualified.
Example
apex
public with sharing class ContactDispatcher implements AfterUpdate.Dispatcher {
public Boolean dispatchOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.Email);
}
public void dispatchOnAfterUpdate(TriggerTypes.UpdateRecords records) {
System.enqueueJob(new ContactSyncJob(records.getIds()));
}
public class ContactSyncJob implements Queueable {
private Set<Id> recordIds;
public ContactSyncJob(Set<Id> recordIds) {
this.recordIds = recordIds;
}
public void execute(QueueableContext context) {
}
}
}apex
public with sharing class OpportunityWonSyncDispatcher implements AfterUpdate.Dispatcher {
public Boolean dispatchOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChangedTo(Opportunity.StageName, 'Closed Won');
}
public void dispatchOnAfterUpdate(TriggerTypes.UpdateRecords records) {
System.enqueueJob(new SyncJob(records.getIds()));
}
public class SyncJob implements Queueable, Database.AllowsCallouts {
private Set<Id> opportunityIds;
public SyncJob(Set<Id> opportunityIds) {
this.opportunityIds = opportunityIds;
}
public void execute(QueueableContext context) {
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:Erp/opportunities');
request.setMethod('POST');
request.setBody(JSON.serialize([SELECT Id, Name, Amount FROM Opportunity WHERE Id IN :this.opportunityIds]));
new Http().send(request);
}
}
}Rules
- One dispatch per chunk. An update of 1,000 records dispatches 5 times, so the example enqueues 5 jobs. A synchronous transaction can enqueue at most 50 Queueable jobs.
- No unit of work. DML in
dispatchOnAfterUpdateruns at once and is not merged with the Writers' changes. DML in the predicate throws. Use a Writer for record changes that commit with the save. - Writers' changes may not be saved yet. By default, their registrations commit after the last handler, so a query here does not see them.
- Writer wins. A class that also implements
AfterUpdate.Writerruns only as a Writer.
WARNING
A callout here throws System.CalloutException. Call out from a Queueable that implements Database.AllowsCallouts, and pass it records.getIds().
Test
apex
@IsTest
static void dispatchOnAfterUpdateWithUnchangedStage() {
// Setup
Opportunity renewal = new Opportunity(StageName = 'Closed Won');
TriggerOrchestrator.mock().afterUpdateFor(OpportunityWonSyncDispatcher.class).with(renewal, new Opportunity(StageName = 'Closed Won'));
// Test
TriggerOrchestrator.runTestFor(new OpportunityWonSyncDispatcher());
// Verify
Assert.areEqual(0, Limits.getQueueableJobs(), 'An opportunity that was already won should not be synced again.');
}