function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Li LeeLi Lee 

Confused by QueryLocator limit in document.

Hi all,
I was investigating Batch Apex recently. I been little confused by Salesforce document about QueryLocator limit.

In "Execution Governors and Limits (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm#!)", it said: "Total number of records retrieved by Database.getQueryLocator 10,000".
But in "Using Batch Apex (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm)", it said: " For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org.".

So I want to ask if I code like follow, and my org have 30,000 account, how many account record return from getQueryLocator, 30000 or 10000? If 10000, how to change code to let it return 30000 recors?
 

public class BatchAccountProccess implements Database.Batchable<sObject> {
    
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT ID,Name,Type FROM Account]);
    }
    
    public void execute(Database.BatchableContext bc, List<Account> records) {
	// do my own process
    }
    
    public void finish(Database.BatchableContext bc) {
	// send email.       
    }
}
Best Answer chosen by Li Lee
ShivankurShivankur (Salesforce Developers) 
Hi Li,

Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and uses the default batch size is considered five transactions of 200 records each.

Start method is used to collect the records or objects to be passed to the interface method execute for processing. This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.

Most of the time a QueryLocator does the trick with a simple SOQL query to generate the scope of objects in the batch job. But if you need to do something crazy like loop through the results of an API call or pre-process records before being passed to the execute method, you might want to check out the Custom Iterators link in the Resources section.

With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.


So, in your example for 30,000 account records, it will query 10,000 recods 3 times and process them asynchrnously by creating sub-jobs internally.

You can get more understanding of these from below links:
https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_batch
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.