---
schema_version: "1"
id: 2cdc00fdc06d708ed80b8dd7996b59c89305f5df22799505cb1a78b9381d8152
title: QueryLocator Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_methods_system_database_batch.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_batch.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 31143919268a0736cf533c99c75c25daaa9b06d8a7be6db2928536756f443a89
description: Represents the record set returned by Database.getQueryLocator and used with Batch Apex.Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.Returns a new instance of a query locator iterator.
---
# QueryLocator Class

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

## Namespace

[Database](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_Database.htm "The Database namespace provides classes used with DML operations.")

## QueryLocator Methods

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

- **[getQuery()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_batch.htm#apex_Database_QueryLocator_getQuery)**  
    Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.
- **[iterator()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_batch.htm#apex_Database_QueryLocator_iterator)**  
    Returns a new instance of a query locator iterator.

### getQuery()

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

#### Signature

public String getQuery()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_methods_system_string.htm)

#### Usage

You can’t use the [FOR UPDATE keywords](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_locking_statements.htm) 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.

#### Example

Assert.areEqual(QLReturnedFromStart.getQuery(),
    Database.getQueryLocator(\[SELECT Id FROM Account\], AccessLevel.USER\_MODE).getQuery());

### iterator()

Returns a new instance of a query locator iterator.

#### Signature

public Database.QueryLocatorIterator iterator()

#### Return Value

Type: [Database.QueryLocatorIterator](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_database_querylocatoriterator.htm#apex_class_database_querylocatoriterator "Represents an iterator over a query locator record set.")

#### Usage

![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note_warning.png)

#### Warning

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_database_querylocatoriterator.htm#apex_class_database_querylocatoriterator "Represents an iterator over a query locator record set.").