Using the Database.QueryLocator
object bypasses the governor limit for the total number of records retrieved by SOQL queries. One could implement one of the following methods to get the QueryLocator:
global static Database.QueryLocator getQueryLocator(List<SObject> query){}
global static Database.QueryLocator getQueryLocator(String query){}
I see many examples in the salesforce site using Database.QueryLocator getQueryLocator(String query)
instead of Database.QueryLocator getQueryLocator(List<SObject> query)
.
Why using Database.QueryLocator getQueryLocator(String query)
? It is easier to make mistakes using a query string. Does Database.QueryLocator getQueryLocator(List<SObject> query)
also retrieve at most 50 million records?
Best Answer
Database.QueryLocator getQueryLocator(List<SObject> query)
only works with an inline query.The following would compile:
The following would NOT compile:
The last example would not compile and salesforce would show the error message:
Why using
Database.QueryLocator getQueryLocator(String query)
?Database.QueryLocator getQueryLocator(String query)
allows code reusability, since once could write a query string in a method and use it elsewhere.Does
Database.QueryLocator getQueryLocator(List<SObject> query)
also retrieve at most 50 million records?