How To Create Leads With Ruby Using the Bulk Salesforce API From the Rails Console

March 05, 2013

How To Create Leads With Ruby Using the Bulk Salesforce API From the Rails Console

Recently, I was looking at using the Salesforce API. Actually, Salesforce has a set of API’s you can interact with, depending on what you need. I was having to think about how to import large numbers of records into Salesforce via their API, so I decided to explore using the Salesforce Bulk API.

What is the Bulk Salesforce API?

This API enables a user to CRUD (create, read, update, delete) large numbers of records asynchronously. In this case “large” can mean something on the order of half a million records or more.

Why Use It?

I chose to look at the Bulk API because I estimated I would have to insert at least 10000 records at any one time, and would have to execute an insertion request at least 20 times in a 24 hour period. If I had used the regular Salesforce Web Services or REST API’s, I likely would have bumped up against their API limits since that would mean doing 10000 individual “insert” calls (mulitplied by 20 times in a 24 hour period).

How Do You Use It?

Step 1: Install the salesforce_bulk gem via bundle install and your Gemfile

I’m assuming you already use rvm and bundler in your rails setup.

There’s a few Salesforce Bulk API gems floating around, but for the purposes of this article, the one you want is at http://rubygems.org/gems/salesforce_bulk.

Step 2: Boot up the rails console

If you’re a Linux or Mac user, typing “rails console” will do the trick.

Step 3: Examples of how to do the queries

Creating New Tasks

First, you’ll have to authenticate to the Salesforce API using your user name, password, and security token as follows:

client = SalesforceBulk::Api.new(“email@example.com”, “my_password”+”security_token”)

*Note that your password is concatenated with your security token. If all goes well, you should get some kind of return object back in your console window.

Next, it’s time to write the Ruby code to create an array of task hashes.

new_tasks = Array.new

10.times do |i|
  new_tasks << {"activitydate" => "2013-1-06", "status" => "Not Started", "subject" => "do me please#{i}"}
end

Then at the prompt:

my_task = sf_client.create("Task", new_tasks, true)

We pass in "true" to indicate that we want to be able to check the status of whether or not the create operation happened.

You should see something like:

=> <SalesforceBulk::Job:0xf0egh70>

@result=

#<SalesforceBulk::JobResult:0xc0ebg35

@errors=[],

@message="The job has been closed.",

@raw=

""00Vi0000001wbXfBEC","true","true",""n"00Vi0000001wbXgBEC","true ","true",""n"00Vi0000001wbXhBEC","true","true",""n"00Vi000000 1wbXiBEC","true","true",""n"00Vi0000001wbXjBEC","true","true", ""n"00Vi0000001wbXkBEC","true","true",""n"00Vi0000001wbXlBEC"," true","true",""n"00Vi0000001wbXmBEC","true","true",""n"00Vi00 00001wbXnBEC","true","true",""n"00Vi0000001wbXoBEC","true","true ",""n",

@records = [#<CSV::Row "Id":"00Vi0000001wbXoBEC" "Success":true "Created":true "Error":"">],

@success=true>>

Creating New Leads

To create new leads in salesforce, two required fields are “company” and “last name” according to the api documentation.

my_leads = Array.new

10.times do |i|
  my_leads<<{&#8216;company&#8217;=>&#8217;boeing&#8217;,&#8217;lastname&#8217;=>"roberts#{i}"}
end

my_result = client.create("Lead",my_leads,true)

=> #<SalesforceBulk::Job:0xcf2fd30

@result = #<SalesforceBulk::JobResult:0xcf2fd08

@errors=[],

@message="The job has been closed.",

@raw=

""01HT0000001XJM8EAO","true","true",""n"01HT0000001XJM9EAO","true ","true",""n"01HT0000001XJMAEA4&#8243;,"true","true",""n"01HT000000 1XJMBEA4&#8243;,"true","true",""n"01HT0000001XJMCEA4&#8243;,"true","true", ""n"01HT0000001XJMDEA4&#8243;,"true","true",""n"01HT0000001XJMEEA4&#8243;," true","true",""n"01HT0000001XJMFEA4&#8243;,"true","true",""n"01HT00 00001XJMGEA4&#8243;,"true","true",""n"01HT0000001XJMHEA4&#8243;,"true","true ",""n",

@records= [#<CSV::Row "Id":"01HT0000001XJMHEA4&#8243; "Success":true "Created":true "Error":"">],

@success=true>>

Now you’re ready to start querying the Salesforce API in your rails application.


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.