Skip to content

AfterDelete.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 AfterDelete.Handler {
    Boolean dispatchOnAfterDeleteWhen(TriggerTypes.DeleteRecord record);
    void dispatchOnAfterDelete(TriggerTypes.DeleteRecords records);
}
  • dispatchOnAfterDeleteWhen(record): called once per record in the chunk. Return true to include the record in the dispatch.
  • dispatchOnAfterDelete(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 AfterDelete.Dispatcher {
    public Boolean dispatchOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isNotNull(Contact.Email);
    }

    public void dispatchOnAfterDelete(TriggerTypes.DeleteRecords records) {
        System.enqueueJob(new ContactSyncJob(records.getIdsOf(Contact.AccountId)));
    }

    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 ContactRemovalSyncDispatcher implements AfterDelete.Dispatcher {
    public Boolean dispatchOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isNotBlank(Contact.Email) && record.isNull(Contact.MasterRecordId);
    }

    public void dispatchOnAfterDelete(TriggerTypes.DeleteRecords records) {
        System.enqueueJob(new RemovalSyncJob(records.getValuesOf(Contact.Email)));
    }

    public class RemovalSyncJob implements Queueable, Database.AllowsCallouts {
        private Set<String> emails;

        public RemovalSyncJob(Set<String> emails) {
            this.emails = emails;
        }

        public void execute(QueueableContext context) {
        }
    }
}

Rules ​

  • No synchronous callouts. A callout from a trigger throws a System.CalloutException. Call out from a Queueable that implements Database.AllowsCallouts.
  • No unit of work. DML in dispatchOnAfterDelete runs at once and is not merged with the Writers' unit. DML in the predicate throws. Use a Writer for record writes.
  • Merge losers arrive here too. Skip them with record.isNull(Contact.MasterRecordId) when a merge should not count as a removal.
  • Writer wins. A class that also implements AfterDelete.Writer runs only as a Writer.

WARNING

Pass values, not the deleted Ids. The job runs after the commit, when the deleted rows can no longer be queried. Pass the values it needs, or the Ids of records that still exist.

Test ​

apex
@IsTest
static void dispatchOnAfterDeleteWithEmail() {
    // Setup
    Contact doe = new Contact(LastName = 'Doe', Email = 'jane.doe@example.com');

    TriggerOrchestrator.mock().afterDeleteFor(ContactDispatcher.class).with(doe);

    // Test
    TriggerOrchestrator.runTestFor(new ContactDispatcher());

    // Verify
    Assert.areEqual(1, Limits.getQueueableJobs(), 'One job should be enqueued for the chunk.');
}