Asynchronous Salesforce DML

Asynchronous Salesforce DML

As of API v49.0, Salesforce apex provides asynchronous DML ability only for external objects. This apex library allows execution of dml for internal object, using queueable interface with features like callback method options. Also, asynchronous mode has increased governor limits, as below:-

Asynchronous mode limits

Even though asynchronous mode has increased limits, but don't forget some limits still apply!

For more details on asynchronous apex, go through the Asynchronous Apex trailhead module.

Github Repository

Get the full code at

Features

  • Perform asynchronous dml operation just like a standard dml operation.

e.g., AsyncDml.insert(accountRecord); or AsyncDml.update(listAccount);

  • Uses reliable Queueable interface. So, its not limited to primitive datatype as @future methods.
  • Allows callback methods to be invoked after dml execution.
  • Allows chaining of queueable jobs.

Usage

Method input parameters

Below are the optional parameters for each operation:-

  • isAllOrNone - To specify whether the operation allows partial success. Default is true.
  • strCallbackMethod - Fully qualified API Name of callback method. The callback class MUST implement Callable interface.

callbackMethod - returns the following parameters:-

  1. jobId - Queueable jobId of the current job.
  2. strStatus - status of job, 'success' or 'failure'.
  3. strErrorMessage - Error message, if any. In case of success, this parameter will be null.
  4. listResult - If 'success' it returns Database.SaveResult[] or Database.DeleteResult[] or Database.UpsertResult[]. In case of 'failure' it returns the input list.
  • chainedJob - The chainedJob to be executed on completion of the current job.


Dummy Data preparation

List<Account> listAccountToInsert = new List<Account>();
  listAccountToInsert.add(
    new Account( name = 'Dummy')
  );

Account accountToUpsert = new Account(
  name='Chained Dummy'
);


Insert List

//this will simply insert the list of accounts as a queueable job
AsyncDml.insertList(listAccountToInsert);


Insert list with allOrNone

//this will simply insert the list of accounts as a queueable job.
AsyncDml.insertList(listAccountToInsert, false);


Insert list with callbackMethod

//this will simply insert the list of accounts as a queueable job.
//After the DML operation AsyncDmlExtension.callbackMethod() method is invoked.
AsyncDml.insertList(listAccountToInsert, null, 'AsyncDmlExtension.callbackMethod');

NOTE

  • callback method can be any method in a class which implements Callable interface. Check AsyncDmlExtension class for example.


Insert list with a chained queueable job

//create a queueable job to be chained
AsyncDmlHelper chainedJob = new AsyncDmlHelper(new List<SObject>{accountToUpsert}, 'upsert', null, null, null);

//this will insert the list of Accounts, & once the insert is finished
//it will enqueue the upsert operation.
AsyncDml.insertList(listAccountToInsert, null, null, chainedJob);

NOTE

  • use any queueable job as a chained job. Chaining queueable methods comes handy when dealing with high volume of records.

CONSIDERATIONS

  • insert, delete & update operations are allowed for either List or an individual record
  • upsert operation is supported ONLY for an individual record.

To view or add a comment, sign in

Others also viewed

Explore content categories