SqlQueueable Class
Abstract base class for executing long-running Data 360 SQL queries asynchronously in chained Queueable jobs. Implements Queueable and Database.AllowsCallouts. Create a concrete subclass that implements processDataChunk() to process each page of results and chainNextJob() to enqueue the next job when more data remains.
The framework handles query submission, status polling, pagination, and job chaining automatically. Your subclass only needs to process data chunks and define how to chain the next job. Inside processDataChunk(), call getRows() to iterate results as Row objects, or getPageOutput() to access the raw ConnectApi.QuerySqlPageOutput. If you call cancel() inside processDataChunk(), the framework skips chainNextJob() automatically.
Define a concrete subclass and start a new query:
The following are constructors for SqlQueueable.
Creates a SqlQueueable subclass instance that resumes an in-progress query from a saved QueryHandle. Use this constructor in chainNextJob() to continue processing the next page.
public SqlQueueable(sfsqlquery.QueryHandle handle)
-
handle:
Type: sfsqlquery.QueryHandle
QueryHandletrackingthe position and state of an in-progress query.
Creates a SqlQueueable subclass instance from a SqlStatement to start a new query. Pass the constructed job to System.enqueueJob() to begin execution.
public SqlQueueable(sfsqlquery.SqlStatement statement)
-
statement:
Type: sfsqlquery.SqlStatement
SqlStatementdefiningthe query to execute.
The following are methods for SqlQueueable.
Cancels the current query. When called inside processDataChunk(), the framework automatically skips chainNextJob(), stopping further job chaining.
public void cancel()
Type: void
Cancel the query based on a condition:
Abstract method called by the framework when more pages remain. Override this in your concrete subclass to enqueue the next job, passing the provided QueryHandle to the next job’s constructor.
public abstract void chainNextJob(sfsqlquery.QueryHandle handle)
-
handle:
Type: sfsqlquery.QueryHandle
QueryHandletrackingthe current query state, including position for the next page. Pass this to your next job’s constructor.
Type: void
Returns the ordered list of column names from the current data chunk. Call this inside processDataChunk().
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 current data chunk, including column names and data types. Call this inside processDataChunk().
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 raw ConnectApi.QuerySqlPageOutput for the current page. Use this inside processDataChunk() when you need direct access to the underlying API response instead of Row objects.
public ConnectApi.QuerySqlPageOutput getPageOutput()
Type: ConnectApi.QuerySqlPageOutput
The raw ConnectApi.QuerySqlPageOutput for the current page.
Access raw API response for custom processing:
Returns the server-assigned ID for this query. Save this value to resume the query in a later job using QueryHandle.create().
public String getQueryId()
Type: String
The server-assigned query ID string.
Save the query ID to resume later:
Returns the rows in the current data chunk as an iterable of Row objects. Call this inside processDataChunk() to iterate over the page with typed accessor methods.
public Iterable<sfsqlquery.Row> getRows()
Type: Iterable
An iterable of Row objects for the current page.
Iterate over rows and insert records:
Abstract method called by the framework with each page of query results. Override this in your concrete subclass to process the data chunk. Use getRows() or getPageOutput() to access the current page’s data.
public abstract void processDataChunk()
Type: void
Returns a string representation of this SqlQueueable.
public String toString()
Type: String
A string representation of this SqlQueueable.