Salesforce APEX - how to perform dml on more than 10000 rows in VF/Apex using Queueable interface

Varun   Varun 2357 Views

A great improvement by salesforce by adding feature like Queueable Interface


Using salesforce Queueable interface you can execute more than 10000 DML row  by creating asynchronous job and you can track status of each job. this is example of divide and concure algorithm.


you can create up to 50 asynchronous jobs, each with 10000 DML rows limit. ( wow )


For Example:


List<String> lstVal1 = new List<String>(); 

List<String> lstVal2 = new List<String>(); 


// add 10K records in each lstVal1 and lastVal2

 

ID jobID1 = System.enqueueJob(new AsyncExecutionExample(lstVal1));

ID jobID2 = System.enqueueJob(new AsyncExecutionExample(lstVal2));

 

Set<Id> lstJbIds = new  Set<Id>();

lstJbIds.add(jobID1);

lstJbIds.add(jobID2);

 

// Now to track jobs status by querying AsyncApexJob from Visualforce page, if you need to track and display result to user, else let jobs run in background and do processing.

 

List<AsyncApexJob> jobInfo = 

    [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id in:lstJbIds];

 

// This example would process total 20K DML rows. Note: this is not complete program

------------------------------------------------------------------------------------------------------------

// Example Queueableinterface implemented class

public class AsyncExecutionExample implements Queueable {


List<String> recordsList = null; 

public AsyncExecutionExample(List<String> listParam){

 this.recordsList = listParam;


public void execute(QueueableContext context) {

   // DML operation on records list here ( insert , delete, update ) total 10K

 

}