Skip to content

AfterDelete.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 bypassOnAfterDeleteWhen();
}
  • bypassOnAfterDeleteWhen(): called once per chunk, before parents load. Returns true to skip this handler for this chunk.

Example

apex
public with sharing class ContactWriter implements AfterDelete.Writer, AfterDelete.Bypassable {
    public static Boolean isBypassed = false;

    public Boolean bypassOnAfterDeleteWhen() {
        return ContactWriter.isBypassed;
    }

    public Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

    public void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getOldSObject()).AccountId, Subject = 'Review contact'));
    }
}

Rules ​

  • To skip single records, use the predicate. Return false from writeOnAfterDeleteWhen or dispatchOnAfterDeleteWhen.
  • Static flags last the whole transaction. Later chunks and nested saves see them too. Reset a flag in a finally block.
  • 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

Outside the error handling. An exception here is not logged, ContinueOnError does not apply, and the delete fails.