Is there any way other than modifying a query in the start method of an apex batch job ex: 'LIMIT 100' to control the amount of records processed by a batch. I know we can use

        Database.executeBatch(myBatchObject, 100);

to control the size of the batch, but is there any way to only have a batch process a certain number of records

Context: I want to test the batch in a production instance before letting it run through several million records at night

2 Answers 2


You have to alter start method , and make that Query Dynamic to accept limits clause from some custom setting or label.

If you don't wanna do that, the only thing I can see, is to start the batch with scope 1, and abort it from the setup screen as soon as you see one chunk has been processed.

 Database.executeBatch(myBatchObject, 1);

I would change start method, as aborting batch in-between kinda breaks the Sanitity of data in production.

  • sanity or sanctity?
    – cropredy
    Jul 23, 2019 at 21:24

Use Database.stateful interface in your batch class, and use an integer as a counter. Increase the counter whenever each record is processed while checking if the counter value is less than or equal the maximum number of amount you want. And keep the max count value in some custom metadata. Something like this in your execute method

integer counter = 0 final integer maxCount = value from metadata; while (counter <= maxCount){ //your logic here counter++ }

You must log in to answer this question.

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