Skip to content

BeforeDelete.Bypassable ​

Skip a handler for the whole chunk when a condition holds, such as a static flag, a batch job or a custom permission.

Signature

apex
public interface Bypassable {
    Boolean bypassOnBeforeDeleteWhen();
}
  • bypassOnBeforeDeleteWhen(): called once per chunk, before parents load. Returns true to skip this handler for this chunk.

Example

apex
public with sharing class ContactValidator implements BeforeDelete.Validator, BeforeDelete.Bypassable {
    public static Boolean isBypassed = false;

    public Boolean bypassOnBeforeDeleteWhen() {
        return ContactValidator.isBypassed;
    }

    public Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isTrue(Contact.DoNotCall);
    }

    public void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record) {
        record.addError('A Do Not Call contact cannot be deleted.');
    }
}

Rules ​

  • To skip single records, use the predicate. Return false from addErrorOnBeforeDeleteWhen or writeOnBeforeDeleteWhen.
  • 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.
  • Only this context. Apex bypasses cover every context, and bypass().handler(X.class) never matches an inner class. A TriggerHandler__mdt record covers the one context it names, for every user.

WARNING

The method runs outside the error handling. An exception in bypassOnBeforeDeleteWhen() is not logged, ContinueOnError does not apply, and the delete fails.