0

I've found documentation on Batch Apex (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm?search_text=batch)

but I'm having trouble parsing through it all to understand how to simply insert in bulk.

I have a . . .

Database.SaveResults[] results = Database.insert(someBigList, false)

. . .that, by the time it gets here, there were ~2300 DML rows left and someBigList was ~3800 rows. My understanding is that Batch Apex could help me get around this DML error it threw.

Can I have some guidance?

Natalie Paige
  • 499
  • 5
  • 19
  • 1
    Batch Apex by itself will not allow you to circumvent the DML rows limit on inserts. If you restructure the process that is generating someBigList into a batch process itself, so that it inserts in smaller batches, that could be a solution. You'd need to add some more detail to your question about the overall objective to see if that's a possibility. – David Reed Apr 17 '18 at 16:01
  • Hmm ok, gotcha. I'm not sure what other information on the use case would clarify next steps. If the goal is simply Database.insert(someBigList, false) , but the list is too large, how could I (if possible?) leverage batch apex or some other bulk method to deliver this result? I simply have a list that at times is too large and need to find a way to insert it, and I know there are methods out there for bulk inserts but can't figure it out – Natalie Paige Apr 17 '18 at 16:07
  • Sorry to double-comment; but when you say "restructure the process . . . into a batch process itself", how do you mean? Restructure it how? – Natalie Paige Apr 17 '18 at 16:12

1 Answers1

1

To get someBigList inserted, you have to iterate over it. This simple generic class can do this:

public class BatchInsert implements Database.Batchable<SObject> {
  SObject[] records;
  public BatchInsert(SObject[] source) {
    records = source;
  }
  public SObject[] start(Database.BatchableContext context) {
    return records;
  }
  public void execute(Database.BatchableContext context, SObject[] scope) {
    Database.insert(scope, false);
  }
  public void finish(Database.BatchableContext context) {
  }
}

Feel free to adjust to meet whatever logging/reporting needs you may have.

This code would be called like:

Database.executeBatch(new BatchInsert(someBigList));
sfdcfox
  • 489,769
  • 21
  • 458
  • 806