SqlQueueable Class
Namespace
Usage
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.
Example
Define a concrete subclass and start a new query:
1public with sharing class MyQueryJob extends sfsqlquery.SqlQueueable {
2 public MyQueryJob(sfsqlquery.SqlStatement stmt) { super(stmt); }
3 public MyQueryJob(sfsqlquery.QueryHandle handle) { super(handle); }
4
5 public override void processDataChunk() {
6 for (sfsqlquery.Row row : getRows()) {
7 System.debug(row.getString('Name__c'));
8 }
9 }
10
11 public override void chainNextJob(sfsqlquery.QueryHandle handle) {
12 System.enqueueJob(new MyQueryJob(handle));
13 }
14}
15
16// Start the job
17sfsqlquery.SqlStatement stmt = sfsqlquery.SqlStatement.create('SELECT Id__c, Name__c FROM accounts__dlm', 'default');
18System.enqueueJob(new MyQueryJob(stmt));SqlQueueable Constructors
The following are constructors for SqlQueueable.
SqlQueueable(handle)
Signature
public SqlQueueable(sfsqlquery.QueryHandle handle)
Parameters
- handle
- Type: sfsqlquery.QueryHandle
- QueryHandle tracking the position and state of an in-progress query.
SqlQueueable(statement)
Signature
public SqlQueueable(sfsqlquery.SqlStatement statement)
Parameters
- statement
- Type: sfsqlquery.SqlStatement
- SqlStatement defining the query to execute.
SqlQueueable Methods
The following are methods for SqlQueueable.
cancel()
Signature
public void cancel()
Return Value
Type: void
Example
Cancel the query based on a condition:
1public override void processDataChunk() {
2 Integer errorCount = 0;
3 for (sfsqlquery.Row row : getRows()) {
4 try {
5 // Process row
6 } catch (Exception e) {
7 errorCount++;
8 if (errorCount > 10) {
9 cancel(); // Stop processing after too many errors
10 return;
11 }
12 }
13 }
14}chainNextJob(handle)
Signature
public abstract void chainNextJob(sfsqlquery.QueryHandle handle)
Parameters
- handle
- Type: sfsqlquery.QueryHandle
- QueryHandle tracking the current query state, including position for the next page. Pass this to your next job's constructor.
Return Value
Type: void
getColumnNames()
Signature
public List<String> getColumnNames()
Example
See getMetadata() for a combined example.
getMetadata()
Signature
public List<ConnectApi.QuerySqlMetadataItem> getMetadata()
Return Value
Type: List<ConnectApi.QuerySqlMetadataItem>
A list of ConnectApi.QuerySqlMetadataItem objects describing each column's name and data type.
Example
Use metadata and column names for dynamic column handling:
1public override void processDataChunk() {
2 List<String> columns = getColumnNames();
3 List<ConnectApi.QuerySqlMetadataItem> metadata = getMetadata();
4
5 System.debug('Query returned ' + columns.size() + ' columns');
6 for (Integer i = 0; i < metadata.size(); i++) {
7 System.debug(columns[i] + ' (' + metadata[i].type + ')');
8 }
9
10 // Process rows using the metadata
11 for (sfsqlquery.Row row : getRows()) {
12 for (String columnName : columns) {
13 Object value = row.getObject(columnName);
14 System.debug(columnName + ': ' + value);
15 }
16 }
17}getPageOutput()
Signature
public ConnectApi.QuerySqlPageOutput getPageOutput()
Return Value
Type: ConnectApi.QuerySqlPageOutput
The raw ConnectApi.QuerySqlPageOutput for the current page.
Example
Access raw API response for custom processing:
1public override void processDataChunk() {
2 ConnectApi.QuerySqlPageOutput output = getPageOutput();
3 System.debug('Page size: ' + output.rows.size());
4 System.debug('Done: ' + output.done);
5}getQueryId()
Signature
public String getQueryId()
Example
Save the query ID to resume later:
1public override void processDataChunk() {
2 String queryId = getQueryId();
3 // Store queryId in a custom object or platform cache
4 MyQueryState__c state = new MyQueryState__c(QueryId__c = queryId);
5 insert state;
6}getRows()
Signature
public Iterable<sfsqlquery.Row> getRows()
Example
Iterate over rows and insert records:
1public override void processDataChunk() {
2 List<Account> accounts = new List<Account>();
3 for (sfsqlquery.Row row : getRows()) {
4 accounts.add(new Account(
5 Name = row.getString('Name__c'),
6 AnnualRevenue = row.getDecimal('Revenue__c')
7 ));
8 }
9 insert accounts;
10}processDataChunk()
Signature
public abstract void processDataChunk()
Return Value
Type: void