SqlRowIterator Class

Synchronous iterator for executing Data 360 SQL queries and retrieving results row-by-row within the current transaction. Implements Iterator<Row> and Iterable<Row> for use in both for-each and while loops. The iterator is single-use; once all rows are consumed, iterating again does not restart from the beginning.

sfsqlquery

Use SqlRowIterator for queries that return manageable result sets within a single transaction’s governor limits. The query isn’t submitted until iteration begins or until getQueryId() is called. For large result sets that may exceed single-transaction governor limits, use SqlQueueable instead.

Basic query using a for-each loop:

Resume from a previously saved query ID:

The following are constructors for SqlRowIterator.

Creates a SqlRowIterator that resumes fetching results for a previously submitted query.

public SqlRowIterator(sfsqlquery.QueryHandle handle)

Creates a SqlRowIterator from a SqlStatement. Equivalent to calling execute() on the statement.

public SqlRowIterator(sfsqlquery.SqlStatement input)

The following are methods for SqlRowIterator.

Cancels the query and releases server-side resources. Any subsequent call to hasNext() returns false.

public void cancel()

Type: void

Cancel the query based on a condition:

Returns the ordered list of column names from the query result set. Available after the first page of results is fetched.

public List<String> getColumnNames()

Type: List<String>

An ordered list of column names as they appear in the result set.

See getMetadata() for a combined example.

Returns the column metadata for the query result set, including column names and their data types. Available after the first page of results is fetched.

public List<ConnectApi.QuerySqlMetadataItem> getMetadata()

Type: List<ConnectApi.QuerySqlMetadataItem>

A list of ConnectApi.QuerySqlMetadataItem objects describing each column’s name and data type.

Use metadata and column names for dynamic column handling:

Returns the server-assigned ID for this query. Triggers query submission if the query hasn’t started yet. Save this value to resume the query in a later transaction using QueryHandle.create().

public String getQueryId()

Type: String

The server-assigned query ID string.

Save query ID for resuming later:

Returns true if more rows remain to be fetched, or false if all rows have been consumed or the query was canceled.

public Boolean hasNext()

Type: Boolean

true if more rows are available; false otherwise.

Use in a while loop:

Returns this SqlRowIterator as an Iterator, enabling use in for-each loops.

public Iterator iterator()

Type: Iterator

This SqlRowIterator instance as an Iterator.

Returns the next Row in the result set. Throws NoSuchElementException if hasNext() is false.

public sfsqlquery.Row next()

Type: sfsqlquery.Row

The next Row in the result set.

Process rows one at a time:

Returns a string representation of this SqlRowIterator.

public String toString()

Type: String

A string representation of this SqlRowIterator.