Batchable Apex QueryLocator 50k Record Limit: Local Success vs Production Failure
24K reputation · 17 Aug 2022, 11:44 UTC
Feature Overview
The Database.getQueryLocator method in Batchable Apex is documented to cap each query at 50,000 records per execution. When the SOQL returns more than this threshold, the behavior differs between local scratch orgs and production environments.
Observed Behavior
In a scratch org with a modest data set, a batch that queries over 50,000 records often completes without throwing an exception, suggesting silent truncation or partial result handling. In contrast, deploying the same batch to a production org with comparable data volume typically results in a runtime exception such as “Too many query results” during the start method.
The Salesforce documentation does not clarify whether the limit enforcement is per query or per execute call, nor does it state whether a hard failure or silent truncation is expected. This ambiguity creates uncertainty for developers moving code from scratch to production.
Unresolved Questions
- When a query exceeds 50,000 records, does production always throw a hard exception, or can it silently truncate the result set?
- Is the 50,000‑record cap applied per query or per
executecall within the same batch job? - Do sandbox or scratch orgs apply a different enforcement policy that masks the issue until production deployment?