Skip to content

AfterUndelete.Bypassable

Skips a Writer or Dispatcher for the whole chunk when a condition holds, such as a static flag or a custom permission.

Interface

apex
public class AfterUndelete {
    public interface Bypassable {
        Boolean bypassOnAfterUndeleteWhen();
    }
}
  • bypassOnAfterUndeleteWhen(): 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 AfterUndelete.Writer, AfterUndelete.Bypassable {
    public static Boolean isDisabled = false;

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

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

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

Good to Know

  • No records. The method answers for the whole chunk. 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.
  • Outside the error handling. An exception here is not logged, ContinueOnError does not apply, and the restore fails.
  • Inner classes. TriggerOrchestrator.bypass().handler(X.class) and .orchestrator(X.class) never match an inner class. Use a TriggerHandler__mdt record or this interface.
  • Only this context. Metadata and bypass().handler(X.class) switch a class off in every context. Implement AfterUndelete.Bypassable to skip only the restore. More switches: Bypassing.