Skip to content

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.

Setup

First, create an apex class that will be used to define your logic. Requirements:

  • Class must be public.
  • Class must have a public 0-arg constructor (either explicit or implicit).
  • Class must implement the Dml.PreAndPostProcessor interface.
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:

image

What Does it Do?

Once set up, the framework will do the following:

  • Call your class's processPreDml method immediately before processing a DML operation
  • Call your class's processPostDml method immediately after processing a DML operation
  • Call your class's processDmlError method if an exception is thrown during a DML operation. After the interface method runs, the exception will be re-thrown.

⚠️ Note: If the DML operation results in an thrown exception (ie., if a failure occurs and allOrNone=true), then the processPostDml method will not be called.

Clone this wiki locally