The Bulk API can also be used to query Force.com to export large numbers of records in a CSV or XML file format. First a bulk export job is created; then a batch is added to the job containing a SOQL statement. The SOQL cannot contain relationship fields; nested queries; or the aggregate functions COUNT
, ROLLUP
, SUM
, or GROUP BY CUBE
. Next, the status of the job is checked, and, finally, the results retrieved in files, each up to 1GB in size.
To begin, create a bulk export job using the request in Listing 11.9.
echo '<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>query</operation>
<object>Project__c</object>
<contentType>CSV</contentType></jobInfo>' |
curl -X POST -H 'Content-type: application/xml'
-H "X-SFDC-Session: "$TOKEN -d @-
https://na15.salesforce.com/services/async/28.0/job
Keep track of the job identifier returned in the response. Create a batch within the job, specifying the SOQL statement. In Listing 11.10, the names and identifiers of the Project records will be exported. Replace JOB_ID
with your job identifier.
echo 'SELECT Id, Name FROM Project__c' |
curl -X POST -H 'Content-type: text/csv'
-H "X-SFDC-Session: "$TOKEN --data-binary @-
https://na15.salesforce.com/services/async/28.0/job/JOB_ID/batch
Make a note of the batch identifier. Use the request in Listing 11.11 to check the status of your export job.
curl https://na15.salesforce.com/services/async/28.0/job/JOB_ID
-H "X-SFDC-Session: "$TOKEN
When the job is complete, the results are ready to retrieve. This is a two-step process. First, retrieve the list of result identifiers. Then, for each result identifier, make a request to retrieve the actual results. Listing 11.12 is an example of the first step. Be sure to replace the JOB_ID
and BATCH_ID
placeholders with your own values.
curl https://na15.salesforce.com/services/async/28.0/
job/JOB_ID/batch/BATCH_ID/result
-H "X-SFDC-Session: "$TOKEN
The last step in the process is shown in Listing 11.13. In addition to job and batch identifiers, replace RESULT_ID
with one of the result identifiers from the prior request.
curl https://na15.salesforce.com/services/async/28.0/
job/JOB_ID/batch/BATCH_ID/result/RESULT_ID
-H "X-SFDC-Session: "$TOKEN
3.147.89.30