Newer Version Available
Running Async SOQL Queries
Formulating Your Async SOQL Query
To use Async SOQL effectively, it’s helpful to understand its key component and other related concepts. Each query is formulated in the POST request as a JSON-encoded list of three or four key-value pairs.
Request body for POST
| Name | Type | Description | Required or Optional | Available Version |
|---|---|---|---|---|
| query | String | Specifies the parameters for the SOQL query you want to execute. | Required | 35.0 |
| operation | String | Specify whether the query is an insert or upsert. If the record doesn’t exist, an upsert behaves like an insert. | Optional | 39.0 |
| targetObject | String | A standard object, custom object, external object, or big object into which to insert the results of the query. | Required | 35.0 |
| targetFieldMap | Map<String, String> | Defines how to map the fields in the query result to the fields in the target object. | Required | 35.0 |
| targetValueMap | Map<String, String> | Defines how to map static strings to fields in the
target object. Any field or alias can be used as the TargetValueMap value in the SELECT clause of a
query. You can map the special value, $JOB_ID, to a field in the target object. The target field must be a lookup to the Background Operation standard object. In this case, the ID of the Background Operation object representing the Async SOQL query is inserted. If the target field is a text field, it must be at least 15–18 characters long. You can also include any field or alias in the SELECT clause of the TargetValueMap. They can be combined together to concatenate a value to be used. |
Optional | 37.0 |
| targetExternalIdField | String | The ID of the target sObject. Required for upsert operations. | Optional | 39.0 |
This simple Async SOQL example queries SourceObject__c, a source custom object, and directs the result to TargetObject__c, another custom object. You can easily map the fields in the source object to the fields of the target object in which you want to write the results.
- Example URI
-
- Example POST request body
-
The response of an Async SOQL query includes the elements of the initial POST request.
Response body for POST
- Example POST response body
-
Tracking the Status of Your Query
- Example GET response body
-
- Example GET response body
-
Canceling a Query
You can cancel a query using an HTTP DELETE request by specifying its jobId.
Handling Errors in Async SOQL Queries
- An error in the query execution
- One or more errors writing the results into the target object
Problems in executing the job cause some errors. For example, an invalid query was submitted, one of the Async SOQL limits was exceeded, or the query caused a problem with the underlying infrastructure. For these errors, the response body includes a status of Failed. The message parameter provides more information on the cause of the failure.
Other times, the query executes successfully but encounters an error while attempting to write the results to the target object. Because of the volume of data involved, capturing every error is inefficient. Instead, subsets of the errors generated are captured and made available. Those errors are captured in the BackgroundOperationResult object and retained for seven days. You can query this object with the Async SOQL query jobID to filter the errors for the specific Async SOQL query. Async SOQL job info is retained for a year.