BeforeInsert.ContinueOnError
Lets a handler fail without failing the insert. Its exception is logged and swallowed, later handlers still run, and the save goes on.
Interface
apex
public class BeforeInsert {
public interface ContinueOnError {
}
}BeforeInsert.ContinueOnError has no methods: implementing it is the whole opt-in.
Example
apex
public with sharing class ContactPopulator implements BeforeInsert.Populator, BeforeInsert.ContinueOnError {
public Boolean populateOnBeforeInsertWhen(TriggerHandler.InsertRecord record) {
return record.isBlank(Contact.LeadSource);
}
public void populateOnBeforeInsert(TriggerHandler.InsertRecord record) {
record.put(Contact.LeadSource, 'Web');
}
}Good to Know
- The handler stops for the chunk. Its remaining records and its Finalizer are skipped. Values it already set and errors it already attached stay.
- On a Validator, the rest goes unchecked. Records after the failing one are saved unless another handler rejects them.
- Some exceptions still fail the insert. DML in the handler, a Validator that attaches no error,
TriggerHandler.TriggerHandlerExceptionandSystem.LimitExceptionare never swallowed. - Only the handler's own code is covered. An exception in
bypassOnBeforeInsertWhen(),queryParentsOnBeforeInsert()or the parent query fails the insert and is not logged. - Implement a Logger to see failures. The library finds your
TriggerOrchestrator.Loggerclass and passes it each swallowed error. Without one, nothing records it. See Errors & Logging.
