2

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

In "Execution Governors and Limits", it said: "Total number of records retrieved by Database.getQueryLocator 10,000".

enter image description here

But in "Using Batch Apex", 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.".

enter image description here

I have no idea which description is correct.

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 records?

My Sample Code

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.       
}

}

4

That limit (10000) would apply to execute and finish methods, as on the start method the limit is bypassed and you can indeed retrieve up to 50 million records. As per the documentation,

When you’re using a simple query (SELECT) to generate the scope of objects in the batch job, use the Database.QueryLocator object. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed.

To answer directly to your question: the getQueryLocator would retrieve 30000 records.

Best regards!

1
  • Thanks,I understand that.
    – GhostLee
    Jul 27 at 8:35
1

10000 limit which you are sharing is limit for Synchronous transactions . Batch Apex run in asynchronous context so there you get limit extended upto 50 million records.

I think with your sample code you wont get any limit issue for 30000 records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.