Skip to content

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

Example

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

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

    public Boolean writeOnAfterUndeleteWhen(TriggerTypes.UndeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

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

Rules ​

  • To skip single records, return false from the predicate.
  • Reset static flags. A flag lasts the whole transaction, nested saves and later chunks included. Reset it 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

An exception in bypassOnAfterUndeleteWhen() is not logged, ContinueOnError does not apply, and the restore fails.