0

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?

1 Answer 1

1

Database.QueryLocator getQueryLocator(List<SObject> query) only works with an inline query.

The following would compile:

public class SuperBatch implements Database.Batchable<SObject>, Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext param1) {
        return Database.getQueryLocator([SELECT Id FROM Account]);
    }

    public void execute(Database.BatchableContext param1, List<Account> accountsList) {
    }

    public void finish(Database.BatchableContext param1) {
    }
}

The following would NOT compile:

public class SuperBatch implements Database.Batchable<SObject>, Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext param1) {
        return Database.getQueryLocator(getAccounts());
    }

    public void execute(Database.BatchableContext param1, List<Account> accountsList) {
    }

    public void finish(Database.BatchableContext param1) {
    }

    private List<Account> getAccounts(){
        return [SELECT Id FROM Account];
    }
}

The last example would not compile and salesforce would show the error message:

Argument must be an inline query

Why using Database.QueryLocator getQueryLocator(String query)?

  • Using 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?

  • Yes, it does.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .