Salesforce bulk API gem

Yatish Mehta
April 19, 2012

Using the Salesforce bulk API for pushing many numbers of records in a single API call.

The official Heroku ‘gem databasedotcom’ which is used as an adapter to talk with the Salesforce’s database doesn’t support bulk API. I did a lot of research, but then I found that ‘gem rforce’ gem uses bulk API, which was not maintained. So finally, I decided to write a gem with help of existing resources where I can use ActiveRecord type methods to access the database along with OAuth2 protocol for authentication. The batch and the job governor limits are also taken care in this gem.

Using the gem is simple and straightforward.

gem install salesforce_bulk_api

or add gem salesforce_bulk_api in your Gemfile.

You can use any method of authentication by using this gem

Please go through the entire documentation of databasedotcom on the various ways of authentication

require 'salesforce_bulk_api'

client = :client_id =>  $SFDC_APP_CONFIG["client_id"], 
                                    :client_secret => $SFDC_APP_CONFIG["client_secret"]
client.authenticate :token => "my-oauth-token", :instance_url => "" 
salesforce =

Once salesforce object is authenticated. It can be used to communicate with Salesforce API.

# Insert/Create
new_account = Hash["name" => "Test Account", "type" => "Other"] # Add as many fields per record as needed.
records_to_insert =
records_to_insert.push(new_account) # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
result = salesforce.create("Account", records_to_insert)
puts "result is: #{result.inspect}"

# Update
updated_account = Hash["name" => "Test Account -- Updated", id => "a00A0001009zA2m"] # Nearly identical to an insert, but we need to pass the salesforce id.
records_to_update =
salesforce.update("Account", records_to_update)

# Upsert
upserted_account = Hash["name" => "Test Account -- Upserted", "External_Field_Name" => "123456"] # Fields to be updated. External field must be included
records_to_upsert =
salesforce.upsert("Account", records_to_upsert, "External_Field_Name") # Note that upsert accepts an extra parameter for the external field name

# Delete
deleted_account = Hash["id" => "a00A0001009zA2m"] # We only specify the id of the records to delete
records_to_delete =
salesforce.delete("Account", records_to_delete)

# Query
res = salesforce.query("Account", "select id, name, createddate from Account limit 3") # We just need to pass the sobject name and the query string

You can find the gem at github You can use the databasedotcom gem for simple purpose i.e. single API calls.

If you would like to reach out to me, I am @yatish27 on Twitter. Feel free to give a shout out.