To import records, an authenticated user creates an import job, adds batches of data to it, closes the job, checks for completion, and then retrieves the results. The results are provided per batch and indicate the status of each imported record. Examples of each step in this process are provided in the remainder of this subsection.
Listing 11.4 creates a bulk import job. It specifies that the records in the job are to be inserted into the Project custom object from a CSV file.
echo '<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>insert</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
Tip
To adapt the command in Listing 11.4 and other listings in this chapter to run in Windows Command Prompt, remove the single quotation mark characters ('
) in the echo
statement, replace the single quotation mark characters around the Content-type
header with double quotation mark characters ("
), remove the backslash () line-continuation characters and concatenate the lines into a single line, and replace
$TOKEN
with _KEN%
.
Make a note of the job identifier, in the id
field of the XML response. It is used in all of the requests that follow. In Listing 11.5, JOB_ID
is a placeholder for the job identifier returned from the import creation request. Replace it with your own. The records in the batch are sent in the body of the request, composed of three Project records with unique names.
echo 'Name
Project1
Project2
Project3' |
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
Save the batch identifier that is returned. You will need it to check for the results of the batch.
You can add more batches to the job by repeating the request. When you’re done adding batches, send the request in Listing 11.6 to close the job, again setting the job identifier to your own. Closing the job signals to Force.com that it can begin processing the job.
echo '<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<state>Closed</state></jobInfo>' |
curl -X POST -H 'Content-type: application/xml'
-H "X-SFDC-Session: "$TOKEN -d @-
https://na15.salesforce.com/services/async/28.0/job/JOB_ID
Job processing is asynchronous, so requests complete immediately but processing continues in the background. To check for the status of the job, send the request in Listing 11.7 with your job identifier.
curl https://na15.salesforce.com/services/async/28.0/job/JOB_ID
-H "X-SFDC-Session: "$TOKEN
When the job is complete, you can retrieve the results of its batches. Each batch result indicates the success or failure of every record within the batch. Listing 11.8 shows a sample request to retrieve the batch status. Replace the job identifier and batch identifier (BATCH_ID
) with your own.
curl https://na15.salesforce.com/services/async/28.0/
job/JOB_ID/batch/BATCH_ID/result
-H "X-SFDC-Session: "$TOKEN
3.145.125.205