I am trying to understand the custom iterators topic Custom Iterators

But there is a thing which I don't get.

There is a next() method which divides the query size into smaller ones, am I right? In the example below there would be several batches containing 8 records.

global Account next(){ 
   // 8 is an arbitrary 
   // constant in this example
   // that represents the 
   // maximum size of the list.
   if(i == 8){return null;} 
   return accs[i-1];

How is that possible to calculate how many records I want to have in one batch?

1 Answer 1


The batch size is specified in Database.executeBatch. Your iterator cannot choose or influence the batch size. See my answer on how batchable uses next and hasNext from a custom iterator to determine when it is done collecting values and how those values are collected. You could, however, return a list of records:

global Account[] next() {
  Account[] records = new Account[8];
  return records;

However, this would act as a multiplier to your execute method:

global void execute(Database.BatchableContext context, List<Account[]> scope) {

Where Database.executeBatch would determine the multiplier. For example, if we do:

Database.executeBatch(new CustomIteratorBatch(), 10);

Given the next method from above, each call to execute would include 80 account records (10 lists of 8 records each).

You must log in to answer this question.

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