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?

3
  • 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, 2018 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 Apr 17, 2018 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? Apr 17, 2018 at 16:12

1 Answer 1

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));
4
  • what is the purpose of "return records" in start(database...) ? where is it returning to? Apr 17, 2018 at 16:22
  • 1
    @NataliePaige It returns the contents of the variable on line 2, which we assigned on line 4 via the constructor, so, in this example, someBigList.
    – sfdcfox
    Apr 17, 2018 at 16:26
  • So it returns automatically to the execute method? Apr 17, 2018 at 16:27
  • 1
    @NataliePaige I wrote an answer that explains the process in a lot more detail than a comment can give you. Give that a read and get back to me if you need any clarification.
    – sfdcfox
    Apr 17, 2018 at 16:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .