QueryLocatorIterator Class

Represents an iterator over a query locator record set.

Namespace

Database

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]);
// 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()

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

Signature

public Boolean hasNext()

Return Value

Type: Boolean

next()

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

Signature

public sObject next()

Return Value

Type: sObject

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();