Skip to content

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

Example

apex
public with sharing class ContactPopulator implements BeforeUpdate.Populator, BeforeUpdate.Bypassable {
    public static Boolean isBypassed = false;

    public Boolean bypassOnBeforeUpdateWhen() {
        return ContactPopulator.isBypassed;
    }

    public Boolean populateOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChanged(Contact.Email);
    }

    public void populateOnBeforeUpdate(TriggerTypes.UpdateRecord record) {
        record.put(Contact.HasOptedOutOfEmail, false);
    }
}
cls
public with sharing class OpportunityReopenValidator implements BeforeUpdate.Validator, BeforeUpdate.Bypassable {
    public static Boolean isBypassed = false;

    public Boolean bypassOnBeforeUpdateWhen() {
        return OpportunityReopenValidator.isBypassed;
    }

    public Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChangedFrom(Opportunity.StageName, 'Closed Won');
    }

    public void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record) {
        Opportunity newOpportunity = (Opportunity) record.getNewSObject();

        record.addError(
            Opportunity.StageName,
            'A Closed Won Opportunity cannot be moved back to ' + newOpportunity.StageName + '. Raise a new Opportunity for the follow-up business.'
        );
    }
}

Rules ​

  • To skip single records, return false from the predicate.
  • Reset a static flag in finally. The flag stays set for every chunk and every nested update in the transaction.
  • 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

Exceptions here are not logged. bypassOnBeforeUpdateWhen() runs outside the handler's error handling. ContinueOnError does not apply, and the update fails.