Loading

Control Number of Batches and Set Scope Size on Apex Batches in Salesforce

Veröffentlichungsdatum: May 1, 2026
Beschreibung

NOTE: The contents of this article apply to batch Apex jobs where the start method returns a query locator for a SOQL query that doesn't contain any inner or subqueries in it.

When a batch Apex job runs with a set batch size, the number of records processed in one batch iteration is not guaranteed to equal the configured scope size (scopeSize). As a result, the total number of batch iterations is not simply the number of records divided by the scope size. This article explains how Salesforce determines the number of execute() iterations based on the retrieve chunk size.

Lösung

Records from the query locator are retrieved in chunks of a given chunk size, called retrieveChunkSize

Retrieve Chunk Sizes

Records from the query locator are retrieved in chunks of a fixed retrieve chunk size (retrieveChunkSize). There are three available values for retrieveChunkSize — 100, 400, and 2000. Salesforce selects the smallest value that is greater than or equal to the configured scope size:

  • If 1 ≤ scopeSize ≤ 100, then retrieveChunkSize = 100
  • If 101 ≤ scopeSize ≤ 400, then retrieveChunkSize = 400
  • If 401 ≤ scopeSize ≤ 2000, then retrieveChunkSize = 2000

Each execute chunk (the records passed to the execute() method) has a size that depends on the chosen retrieveChunkSize, the scopeSize, and the available records in the current retrieve chunk.

Worked Example: 290 Records with Scope Size 75

Given: 290 records, scopeSize = 75, therefore retrieveChunkSize = 100. This produces 3 retrieve chunks and 6 execute chunks:
Retrieve Chunk 1 (first 100 records):

  • Execute Chunk 1: first 75 records passed to execute()
  • Execute Chunk 2: remaining 25 records passed to execute()

Retrieve Chunk 2 (next 100 records):

  • Execute Chunk 1: first 75 records of this retrieve chunk passed to execute()
  • Execute Chunk 2: remaining 25 records passed to execute()

Retrieve Chunk 3 (remaining 90 records):

  • Execute Chunk 1: first 75 records of this retrieve chunk passed to execute()
  • Execute Chunk 2: remaining 15 records passed to execute()

Total batches = 6, not ceiling(290/75) = 4. When a retrieve chunk contains the full 100 records, it is processed in two execute() calls of 75 and 25 records respectively.

Nummer des Knowledge-Artikels

000381863

 
Laden
Salesforce Help | Article