Batch Operation Limit

Is there a limit to the operations allowed for batch?

Hi @Dee

There is no logical limit to the number of operations that you can add and then execute in a Batch, but there are practical ones, like memory.

That said, the platform will automatically split the operations into groups of 1000 if you try to execute more than 1000 operations in a single batch. This means if you have 1800 operations in your batch at the time of batch execution the platform will apply those operations in 2 groups, first a group of 1000 and then a group of 800. You don’t have to do anything to account for this.

But, and this is especially true in CC, it is recommended that you execute and then clear your batch at least every 1000 operations, if not sooner, to avoid running into memory issues. (This could be the case if the objects you are pushing into the batch are large, ie have lots of fields)

For example, if you are expecting to have a total of 10000 operations that you need to execute, then it is recommended that you manually execute them in batches of 1000, clearing the batch after every execution, to avoid having a single batch variable that contains 10000 operations.

Something like this.

let batch = new DB.Batch();
let operations = await DB.operations.all().toArray(); // assume 10000 ops
for (let i = 0; i < operations.length; i++) {
  let op = operations[i];
  op.updated = true;
  batch.save(op);

  // lets execute and clear the batch every 1000 iterations
  // the logic assume only 1 operation per iteration, if you are doing than 1 operation per iteration then simply keep track of the number of operations in the batch in a variable
  // also execute if it's the last iteration
  if ((i+1)%1000 == 0 || i+1 == operations.length) {
    await batch.execute();
    // we create a new batch if we expect another iteration
    if (i+1 < operations.length) {
      batch = new DB.Batch()
    }
  }
}