-
Notifications
You must be signed in to change notification settings - Fork 0
Plugin: Dml.PreAndPostProcessor
Jason Siders edited this page Aug 19, 2025
·
9 revisions
This plugin allows developers to define logic to run immediately before, and/or immediately after a DML operation runs. This can be used for specialized functions, like logging.
First, create an apex class that will be used to define your logic. Requirements:
- Class must be
public. - Class must have a
public0-arg constructor (either explicit or implicit). - Class must implement the
Dml.PreAndPostProcessorinterface.
public class SomeApexClass implements Dml.PreAndPostProcessor {
// This sample PreAndPostProcessor logs DML operations, using Nebula Logger:
public void processPreDml(Dml.Request request) {
Logger.finest('About to process ' + this.getLogSuffix(request))?.setRecord(request?.records);
Logger.finest(msg)?.setRecord(request?.records);
}
public void processPostDml(Dml.Request request, List<Object> results) {
Logger.finest('Processed ' + this.getLogSuffix(request))?.setRecord(request?.records);
}
public void processDmlError(Dml.Request request, Exception error) {
String msg = request?.operation + ' error: ' + error;
Logger.error(msg)?.setExceptionDetails(error);
Logger.saveLog();
}
private String getLogSuffix(Dml.Request req) {
return req?.numRecords + ' ' + req?.sObjectType + ' records. Operation: ' + req?.operation;
}
}Next, create a Database Layer Setting/DatabaseLayerSetting__mdt custom metadata record, called "Default" (unless one already exists).
Finally, list your apex class from the previous step in the DML: Pre & Post Processor field, as shown below:
Once set up, the framework will do the following:
- Call your class's
processPreDmlmethod immediately before processing a DML operation - Call your class's
processPostDmlmethod immediately after processing a DML operation - Call your class's
processDmlErrormethod if an exception is thrown during a DML operation. After the interface method runs, the exception will be re-thrown.
allOrNone=true), then the processPostDml method will not be called.
- Generating Test Records
- Dml
- Soql
- Cmdt
- Plugins
- DatabaseLayer
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- MockSoql
-
Soql
- Soql.AggregateResult
- Soql.Aggregation
- Soql.Binder
- Soql.Builder
- Soql.Condition
- Soql.ConditionalLogic
- Soql.Criteria
- Soql.Cursor
- Soql.Function
- Soql.InnerQuery
- Soql.InvalidParameterValueException
- Soql.LogicType
- Soql.NullOrder
- Soql.Operation
- Soql.Operator
- Soql.ParentField
- Soql.PreAndPostProcessor
- Soql.QueryLocator
- Soql.Request
- Soql.Scope
- Soql.Selectable
- Soql.SortDirection
- Soql.SortOrder
- Soql.Subquery
- Soql.TypeOf
- Soql.Usage
- Soql.WhenClause