Salesforce Bulk API

In this post, I am going to show how to use Salesforce Bulk API which provides a programmatic option to quickly load your org’s data into Salesforce.
With the Bulk API, You process a set of records by creating a job that contains one or more batches. The job specifies which object is being processed and what type of operation is being used. A batch is a set of records sent to the server to process and Each batch is processed independently by the server, not necessarily in the order it is received. Batches may be processed in parallel or concurrent.  Here are the details steps to load the data with bulk API

Step 1: Log In Using the SOAP API

The Bulk API doesn’t provide a login operation, so you must use SOAP API to log in.Create a text file called login.txt containing the following text and replace the username and password.

Run the following command from the cURL command line.

Which will return the an XML response that includes <sessionId> and elements. We are going to use the sessionId for the subsequent  request authentication.

Step 2: Create a Job

Before you can load the data, you need to create a job. The job specifies the type of object, such as Contact, that you’re loading and the operation that you’re performing, such as query, queryAll, insert, update, upsert, or delete.  Create a text file called job.txt containing the following text.

Now Run the following command from the cURL command line

You will get the response as shown below. In this response, you will get the jobId which we will use in subsequence calls.

 

Step 3: Add a Batch to the Job

After creating the job, you need to create the batch that contains the data to be loaded into the salesforce.  Create a CSV file that contains the data. Using a command-line window, execute the following cURL command.

Which will return the batchId as shown below.

Step 4: Close the Job

When you’re finished submitting batches to Salesforce, close the job. This informs Salesforce that no more batches will be submitted for the job, which, in turn, allows the monitoring page in Salesforce to return more meaningful statistics on the progress of the job.Create a text file called close_job.txt containing the following text.

Run the following command from cURL

Which will return the batch details and update the job resource state from Open to Closed.

Step 5: Check Batch Status

You can check the status of an individual batch by running the following cURL command.

which will return the following XML response with the status like Failed or completed.

Step 6: Retrieve Batch Results

Once a batch is Completed, you need to retrieve the batch result to see the status of individual records. Retrieve the results for an individual batch by running the following cURL command:

Salesforce returns a response with data such as the following