QueryLocator Class

Represents the record set returned by Database.getQueryLocator and used with Batch Apex.

Database

The following are methods for QueryLocator. All are instance methods.

  • getQuery()

    Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.

  • iterator()

    Returns a new instance of a query locator iterator.

Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.

public String getQuery()

Type: String

You can’t use the FOR UPDATE keywords with a getQueryLocator query to lock a set of records. The set of records in the batch is determined when the start method is run.

Returns a new instance of a query locator iterator.

public Database.QueryLocatorIterator iterator()

Type: Database.QueryLocatorIterator

To iterate over a query locator, save the iterator instance that this method returns in a variable and then use this variable to iterate over the collection. Calling iterator every time you want to perform an iteration can result in incorrect behavior because each call returns a new iterator instance.

For an example, see QueryLocatorIterator Class.