---
schema_version: "1"
id: a5c6f38e138391c474efff253c9d9f018547f5f5a3aaba5204699d8001988bfe
title: QueryLocatorIterator Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_database_querylocatoriterator.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_database_querylocatoriterator.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: b615dc90cb45d51e8f70288c70508ebfd7c9454dada173dc56dfcf7fa35ff3bf
description: Represents an iterator over a query locator record set.Returns true if there are one or more records remaining in the collection; otherwise, returns false.Advances the iterator to the next sObject record and returns the sObject.
---
# QueryLocatorIterator Class

Represents an iterator over a query locator record set.

## 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.")

## Example

This sample shows how to obtain an iterator for a query locator, which contains five accounts. This sample calls hasNext and next to get each record in the collection.

// Get a query locator
Database.QueryLocator q = Database.getQueryLocator(
    \[SELECT Name FROM Account LIMIT 5\], AccessLevel.USER\_MODE);
// Get an iterator
Database.QueryLocatorIterator it =  q.iterator();
 
// Iterate over the records
while (it.hasNext())
{
    Account a = (Account)it.next();
    System.debug(a);
}

## QueryLocatorIterator Methods

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

- **[hasNext()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_database_querylocatoriterator.htm#apex_Database_QueryLocatorIterator_hasNext)**  
    Returns true if there are one or more records remaining in the collection; otherwise, returns false.
- **[next()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_database_querylocatoriterator.htm#apex_Database_QueryLocatorIterator_next)**  
    Advances the iterator to the next sObject record and returns the sObject.

### hasNext()

Returns true if there are one or more records remaining in the collection; otherwise, returns false.

#### Signature

public Boolean hasNext()

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

### next()

Advances the iterator to the next sObject record and returns the sObject.

#### Signature

public sObject next()

#### Return Value

Type: [sObject](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Usage

Because the return value is the generic sObject type, you must cast it if using a more specific type. For example:

Account a = (Account)myIterator.next();

#### Example

Account a = (Account)myIterator.next();