AfterDelete.Bypassable
Skips a Writer or Dispatcher when a condition holds, for example during a batch purge. Read a static flag, a custom permission or System.isBatch().
Interface
apex
public class AfterDelete {
public interface Bypassable {
Boolean bypassOnAfterDeleteWhen();
}
}bypassOnAfterDeleteWhen(): called once per chunk, before parents load; not called when metadata orTriggerOrchestrator.bypass()already skips the handler. Returnstrueto skip this handler for this chunk.
Example
apex
public with sharing class ContactWriter implements AfterDelete.Writer, AfterDelete.Bypassable {
public static Boolean isDisabled = false;
public Boolean bypassOnAfterDeleteWhen() {
return ContactWriter.isDisabled;
}
public Boolean writeOnAfterDeleteWhen(TriggerHandler.DeleteRecord record) {
return record.isNotNull(Contact.AccountId);
}
public void writeOnAfterDelete(TriggerHandler.DeleteRecord record, TriggerHandler.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getOldSObject()).AccountId, Subject = 'Review contact'));
}
}Good to Know
- To skip single records, use the predicate. Return false from
writeOnAfterDeleteWhenordispatchOnAfterDeleteWhen. - Outside the error handling. An exception here is not logged, ContinueOnError does not apply, and the delete fails.
- Static flags last the whole transaction. Later chunks and nested saves see them too. Reset a flag in a
finallyblock. - Other switches apply in every context.
TriggerOrchestrator.bypass().handler(ContactWriter.class)works for one transaction; aTriggerHandler__mdtrecord works org-wide. See Bypassing. - Inner classes never match by class.
bypass().handler(X.class)and.orchestrator(X.class)ignore inner classes. Use aTriggerHandler__mdtrecord or this add-on.
