Skip to content

BeforeDelete.ContinueOnError

Logs and swallows the handler's exceptions, so the other handlers and the delete go on. Use it for cleanup that must not block a delete.

Interface

apex
public class BeforeDelete {
    public interface ContinueOnError {
    }
}

BeforeDelete.ContinueOnError has no methods: implementing it is the whole opt-in.

Example

apex
public with sharing class ContactHandler implements BeforeDelete.Handler, BeforeDelete.ContinueOnError {
    public Boolean qualifiesForBeforeDeleteWhen(TriggerHandler.DeleteRecord record) {
        return record.isTrue(Contact.DoNotCall);
    }

    public void onBeforeDelete(TriggerHandler.DeleteRecord record) {
        record.getOldSObject().addError('A Do Not Call contact cannot be deleted.');
    }
}

Good to Know

  • Never on a guard. A swallowed exception means no error is attached, so the record is deleted.
  • The rest of the handler is skipped. After an exception, this handler's remaining records and its Finalizer are skipped. An exception in a provider skips every record. Later handlers still run.
  • No rollback. Errors already attached and DML statements that already ran stay. The library sets no savepoint.
  • Some exceptions always throw. TriggerHandler.TriggerHandlerException and TriggerOrchestratorException are rethrown. System.LimitException cannot be caught. Exceptions in bypassOnBeforeDeleteWhen() and queryParentsOnBeforeDelete() fail the delete.
  • Add a Logger. The exception goes to the org's TriggerOrchestrator.Logger. Without one it leaves no trace. See Errors & Logging.