Skip to content

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

InterfaceBefore InsertAfter InsertBefore UpdateAfter UpdateBefore DeleteAfter DeleteAfter Undelete
Handler
NewRecordEnrichment
OldRecordEnrichment
Bypassable
RecursionGuard
Finalizer
AllowDmls
ContinueOnError

Handler

Required. Qualifies records and processes each qualified one.

apex
public interface Handler {
  Boolean qualifiesForBeforeInsertWhen(TriggerHandler.Record record);
  void onBeforeInsert(TriggerHandler.Record record);
}
ContextMethods
Before InsertqualifiesForBeforeInsertWhen, onBeforeInsert
After InsertqualifiesForAfterInsertWhen, onAfterInsert
Before UpdatequalifiesForBeforeUpdateWhen, onBeforeUpdate
After UpdatequalifiesForAfterUpdateWhen, onAfterUpdate
Before DeletequalifiesForBeforeDeleteWhen, onBeforeDelete
After DeletequalifiesForAfterDeleteWhen, onAfterDelete
After UndeletequalifiesForAfterUndeleteWhen, 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).

apex
public interface NewRecordEnrichment {
  Map<SObjectField, TriggerHandler.FieldSelection> newFieldsToEnrichOnBeforeInsert();
}
ContextMethod
Before InsertnewFieldsToEnrichOnBeforeInsert
After InsertnewFieldsToEnrichOnAfterInsert
Before UpdatenewFieldsToEnrichOnBeforeUpdate
After UpdatenewFieldsToEnrichOnAfterUpdate
After UndeletenewFieldsToEnrichOnAfterUndelete

See Parent Enrichment.

OldRecordEnrichment

Declares parent fields to query for the old version of each record. Read them with record.getOldRelated(relationshipName).

apex
public interface OldRecordEnrichment {
  Map<SObjectField, TriggerHandler.FieldSelection> oldFieldsToEnrichOnBeforeUpdate();
}
ContextMethod
Before UpdateoldFieldsToEnrichOnBeforeUpdate
After UpdateoldFieldsToEnrichOnAfterUpdate
Before DeleteoldFieldsToEnrichOnBeforeDelete
After DeleteoldFieldsToEnrichOnAfterDelete

Bypassable

Skips the handler for the current invocation when the method returns true.

apex
public interface Bypassable {
  Boolean bypassOnBeforeInsertWhen();
}
ContextMethod
Before InsertbypassOnBeforeInsertWhen
After InsertbypassOnAfterInsertWhen
Before UpdatebypassOnBeforeUpdateWhen
After UpdatebypassOnAfterUpdateWhen
Before DeletebypassOnBeforeDeleteWhen
After DeletebypassOnAfterDeleteWhen
After UndeletebypassOnAfterUndeleteWhen

See Bypasses.

RecursionGuard

Overrides the default depth of 3 for update contexts.

apex
public interface RecursionGuard {
  Integer maxRecursionDepthOnBeforeUpdate();
}
ContextMethod
Before UpdatemaxRecursionDepthOnBeforeUpdate
After UpdatemaxRecursionDepthOnAfterUpdate

See Recursion Control.

Finalizer

Runs once after all qualified records were processed.

apex
public interface Finalizer {
  void finalizeBeforeInsert();
}
ContextMethod
Before InsertfinalizeBeforeInsert
After InsertfinalizeAfterInsert
Before UpdatefinalizeBeforeUpdate
After UpdatefinalizeAfterUpdate
Before DeletefinalizeBeforeDelete
After DeletefinalizeAfterDelete
After UndeletefinalizeAfterUndelete

See Finalizers.

AllowDmls

Marker interface. Disables the DML guard for a before insert or before update handler.

apex
public interface AllowDmls {
}

See Before Context DML.

ContinueOnError

Marker interface. Exceptions from the handler are logged and the orchestrator continues with the next handler.

apex
public interface ContinueOnError {
}

See Error Handling.