- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
The Dml.PreAndPostProcessor Interface
This interface defines logic to be run before/after DML operations, as well as when an exception is thrown during a DML operation. You can use this for logging, or other specialized use cases.
All interface methods include a Dml.Request object. This object includes contextual information about the operation to be processed. See the Dml.Request class for the full list of parameters.
Read more about how this plugin works here.
Example:
public class MyPlugin 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;
  }
}Defines logic to run when an exception is thrown during a Dml operation.
Once this method runs, the framework will re-throw the Exception. If this is not desired, wrap your DML operation in a try/catch block. You cannot do this from within the interface itself.
Note: This message does not handle partial success DML operations, since failures here do not yield an Exception. To handle these operations, use the processPostDml method and the isSuccess() method on the database result objects.
void processDmlError(Dml.Request request, Exception error)
Defines logic to be run immediately before a DML operation.
void processPreDml(Dml.Request request)
Defines logic to be run immediately before a DML operation.
Also includes a List<Object>, which represents the Database results (ie., Database.SaveResult, Database.DeleteResult, Database.UpsertResult, etc) produced by the DML operation. Since these objects do not share a common interface, the return type must be a generic List<Object>.
Note: This method is not called if the DML operation yields a thrown exception. You can use the processDmlError method to handle these errors instead.
void processPostDml(Dml.Request request, List<Object> databaseResults)
operation being processed:
| DML Operation | Database Result Type | 
|---|---|
DO_CONVERT | 
    Database.ConvertResult | 
  
DO_INSERT, DO_PUBLISH, DO_UPDATE
 | 
    Database.SaveResult | 
  
DO_PURGE | 
    Database.EmptyRecycleBinResult | 
  
DO_UNDELETE | 
    Database.UndeleteResult | 
  
DO_UPSERT | 
    Database.UpsertResult | 
  
- 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