Skip to content

BeforeDelete.Bypassable

Skips a before delete handler when a condition holds, such as a static flag during a batch purge.

Interface

apex
public class BeforeDelete {
    public interface Bypassable {
        Boolean bypassOnBeforeDeleteWhen();
    }
}
  • bypassOnBeforeDeleteWhen(): 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 ContactHandler implements BeforeDelete.Handler, BeforeDelete.Bypassable {
    public static Boolean isDisabled = false;

    public Boolean bypassOnBeforeDeleteWhen() {
        return ContactHandler.isDisabled;
    }

    public Boolean qualifiesForBeforeDeleteWhen(TriggerHandler.DeleteRecord record) {
        return record.isTrue(Contact.DoNotCall);
    }

    public void onBeforeDelete(TriggerHandler.DeleteRecord record) {
        record.getOldSObject().addError('A Do Not Call contact cannot be deleted.');
    }
}

Good to Know

  • No records. The method decides for the whole chunk. To skip only some records, return false from qualifiesForBeforeDeleteWhen.
  • Outside the error handling. An exception in bypassOnBeforeDeleteWhen() is not logged, ContinueOnError does not apply, and the delete fails.
  • Reset static flags. A static flag lasts the whole transaction, nested saves included. Reset it in a finally block after the DML it was meant for.
  • A bypassed guard blocks nothing. While a handler that blocks deletes is switched off, those deletes go through. An object-wide switch has the same effect.
  • Inner classes need metadata. TriggerOrchestrator.bypass().handler(X.class) never matches an inner class. Use a TriggerHandler__mdt record or this add-on instead. See Bypassing.