Skip to content

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 or TriggerOrchestrator.bypass() already skips the handler. Returns true to 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 writeOnAfterDeleteWhen or dispatchOnAfterDeleteWhen.
  • 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 finally block.
  • Other switches apply in every context. TriggerOrchestrator.bypass().handler(ContactWriter.class) works for one transaction; a TriggerHandler__mdt record works org-wide. See Bypassing.
  • Inner classes never match by class. bypass().handler(X.class) and .orchestrator(X.class) ignore inner classes. Use a TriggerHandler__mdt record or this add-on.