-4

Can we change default scope?How?.

1
  • 4
    Downvoted for lack of research
    – Derek F
    Commented Feb 17, 2023 at 15:11

2 Answers 2

3

This is covered in the documentation on Using Batch Apex

The Database.executeBatch method takes two parameters:

  • An instance of a class that implements the Database.Batchable interface.
  • An optional parameter scope. This parameter specifies the number of records to pass into the execute method. Use this parameter when you have many operations for each record being passed in and are running into governor limits. By limiting the number of records, you’re limiting the operations per transaction. This value must be greater than zero. If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method of the batch class returns an iterable, the scope parameter value has no upper limit. However, if you use a high number, you can run into other limits. The optimal scope size is a factor of 2000, for example, 100, 200, 400 and so on.

Also from that documentation page

If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200 records. The system then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.

So to sum up:

  • Use the 2 parameter version of Database.executeBatch() to set the scope size
  • If you don't provide a scope size, the default scope size is 200
  • The maximum scope size is 2,000
    • ...unless you're using an Iterable, in which case there is no max scope size (but you are also limited to 50,000 query rows in the start() method)
  • In all cases, you are subject to the governor limits (async) in each execution of execute()
2

For the scope size, the default is 200, the maximum is 2000, and the minimum is 1. The scope size is chosen at the moment the batch is queued for execution:

Database.executeBatch(myBatch); // 200 scope size
Database.executeBatch(myOtherBatch, 2000); // maximum batch size

There is no opportunity to change the batch size from within the batch itself, and the default value will always be 200.

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