Context Interfaces
Each trigger context has its own top-level class that groups the interfaces a handler can implement: BeforeInsert, AfterInsert, BeforeUpdate, AfterUpdate, BeforeDelete, AfterDelete and AfterUndelete.
Method names carry the context name, so one class can implement several contexts without clashes.
Availability Matrix
| Interface | Before Insert | After Insert | Before Update | After Update | Before Delete | After Delete | After Undelete |
|---|---|---|---|---|---|---|---|
Handler | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
NewRecordEnrichment | ✅ | ✅ | ✅ | ✅ | ✅ | ||
OldRecordEnrichment | ✅ | ✅ | ✅ | ✅ | |||
Bypassable | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
RecursionGuard | ✅ | ✅ | |||||
Finalizer | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
AllowDmls | ✅ | ✅ | |||||
ContinueOnError | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Handler
Required. Qualifies records and processes each qualified one.
public interface Handler {
Boolean qualifiesForBeforeInsertWhen(TriggerHandler.Record record);
void onBeforeInsert(TriggerHandler.Record record);
}| Context | Methods |
|---|---|
| Before Insert | qualifiesForBeforeInsertWhen, onBeforeInsert |
| After Insert | qualifiesForAfterInsertWhen, onAfterInsert |
| Before Update | qualifiesForBeforeUpdateWhen, onBeforeUpdate |
| After Update | qualifiesForAfterUpdateWhen, onAfterUpdate |
| Before Delete | qualifiesForBeforeDeleteWhen, onBeforeDelete |
| After Delete | qualifiesForAfterDeleteWhen, onAfterDelete |
| After Undelete | qualifiesForAfterUndeleteWhen, onAfterUndelete |
See Handlers and Record Qualification.
NewRecordEnrichment
Declares parent fields to query for the new version of each record. Read them with record.getNewRelated(relationshipName).
public interface NewRecordEnrichment {
Map<SObjectField, TriggerHandler.FieldSelection> newFieldsToEnrichOnBeforeInsert();
}| Context | Method |
|---|---|
| Before Insert | newFieldsToEnrichOnBeforeInsert |
| After Insert | newFieldsToEnrichOnAfterInsert |
| Before Update | newFieldsToEnrichOnBeforeUpdate |
| After Update | newFieldsToEnrichOnAfterUpdate |
| After Undelete | newFieldsToEnrichOnAfterUndelete |
See Parent Enrichment.
OldRecordEnrichment
Declares parent fields to query for the old version of each record. Read them with record.getOldRelated(relationshipName).
public interface OldRecordEnrichment {
Map<SObjectField, TriggerHandler.FieldSelection> oldFieldsToEnrichOnBeforeUpdate();
}| Context | Method |
|---|---|
| Before Update | oldFieldsToEnrichOnBeforeUpdate |
| After Update | oldFieldsToEnrichOnAfterUpdate |
| Before Delete | oldFieldsToEnrichOnBeforeDelete |
| After Delete | oldFieldsToEnrichOnAfterDelete |
Bypassable
Skips the handler for the current invocation when the method returns true.
public interface Bypassable {
Boolean bypassOnBeforeInsertWhen();
}| Context | Method |
|---|---|
| Before Insert | bypassOnBeforeInsertWhen |
| After Insert | bypassOnAfterInsertWhen |
| Before Update | bypassOnBeforeUpdateWhen |
| After Update | bypassOnAfterUpdateWhen |
| Before Delete | bypassOnBeforeDeleteWhen |
| After Delete | bypassOnAfterDeleteWhen |
| After Undelete | bypassOnAfterUndeleteWhen |
See Bypasses.
RecursionGuard
Overrides the default depth of 3 for update contexts.
public interface RecursionGuard {
Integer maxRecursionDepthOnBeforeUpdate();
}| Context | Method |
|---|---|
| Before Update | maxRecursionDepthOnBeforeUpdate |
| After Update | maxRecursionDepthOnAfterUpdate |
See Recursion Control.
Finalizer
Runs once after all qualified records were processed.
public interface Finalizer {
void finalizeBeforeInsert();
}| Context | Method |
|---|---|
| Before Insert | finalizeBeforeInsert |
| After Insert | finalizeAfterInsert |
| Before Update | finalizeBeforeUpdate |
| After Update | finalizeAfterUpdate |
| Before Delete | finalizeBeforeDelete |
| After Delete | finalizeAfterDelete |
| After Undelete | finalizeAfterUndelete |
See Finalizers.
AllowDmls
Marker interface. Disables the DML guard for a before insert or before update handler.
public interface AllowDmls {
}See Before Context DML.
ContinueOnError
Marker interface. Exceptions from the handler are logged and the orchestrator continues with the next handler.
public interface ContinueOnError {
}See Error Handling.
