Exporting Records

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.

Listing 11.9 Creating the Bulk Export Job


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.

Listing 11.10 Creating the Bulk Export Batch


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.

Listing 11.11 Checking the Status of the Bulk 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.

Listing 11.12 Retrieving Result Identifiers of the Bulk Export Job


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.

Listing 11.13 Retrieving Results of the Bulk Export Job


curl https://na15.salesforce.com/services/async/28.0/
job/JOB_ID/batch/BATCH_ID/result/RESULT_ID
  -H "X-SFDC-Session: "$TOKEN


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.121.156