0

My goal is to create a reusable and scalable utility class that can perform different DML operations. I'm aware of the 10,000 DML processed records limit so I'd like to check if the list is greater than that number, split into groups of 10k until we reach a group smaller than that limit.

I would be able to accomplish this by creating pre-assumed lists but I would like this to be fully dynamic and have become stuck.

Could I get some assistance / information on how best to approach this efficiently?

I have trimmed out other operations and have just left 'Insert'

DMLUtility.cls

public class DMLUtility {


public static DMLQueueable dmlQueueable;
public static Boolean Status;


public static void addForInsert(List<sObject> sObjects){ 

    if(dmlQueueable == null) {
        dmlQueueable = new DMLQueueable();
    }
    dmlQueueable.recordListInsert.addall(sObjects);

}



public static Boolean execute() {
    System.enqueueJob(dmlQueueable);

    return Status;
}
}

DMLQueueable.cls

public class DMLQueueable implements Queueable {

 public List<sObject> recordListInsert;


 public DMLQueueable(){

     recordListInsert = new List<sObject>();

 }


  public void execute(QueueableContext context){


    if(recordListInsert.size){ // If size is present (greater than null or zero)
      for(Integer i = 0; i < 10000; i++){
        // This is where I'd like to loop through recordListInsert and dynamically split into groups of 10k or less
      }

    }
}
2

Aside from the potential problems with governor limits, getting your 10k lists is pretty simple:

sObject[] dmlExecuteList = new sObject[0];
while(!recordListInsert.isEmpty() && dmlExecuteList.size() < 10000) {
  // Removes first item from recordListInsert and adds it to the dmlExecuteList
  dmlExecuteList.add(recordListInsert.remove(0));
}
insert dmlExecuteList;
// If we're not done, go to the next batch of records
if(!recordListInsert.isEmpty()) {
  System.enqueueJob(this);
}
  • Very elegant, didn't think of just using a while loop! – David Picksley Jan 19 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.