Connecting Ruby on Rails with Salesforce.com

by - 10:40 PM

While experimenting with Salesforce and Heroku, I thought of connecting both the cloud with each other so as to increase the scalability to the project. So I researched about  the connecting the cloud and I found couple of outstanding resources that help me in achieving this.


How to connect ?
To connect both the clouds what we need here is an adapter that do the connection between two frameworks and allow intercommunication between these two clouds. Rather than writing this adapter from scratch, I lately learned that RubyGems had developed an application and published over App-exchange that works as adapter for the cloud framework connection.

ActiveSalesforce and Rforce are the application available and managed through appexchange which uses Rail's record model layer to simplify the access to Salesforce both the custom and standard fields and objects. This also includes Salesforce.com scaffold generator that leverages other important features of Salesforce. Clearly the  life gets little easier now.

To get started follow the steps :

Open up command prompt in windows ( I am trying with windows only)

$ gem install  rforce  




Now move back to Salesforce to create or reset Security Token as shown in the picture below


















Below is the code sample required to run the test to retrieve data from Salesforce, the sample source code credit for test case is partially developed by me another blog 'DeveloperFart' had improved version which made me to make the required changes and share here.


  1. require 'test/unit'
  2. require 'rubygems'
  3. gem 'rforce'
  4. require 'rforce'
  5. include RForce
  6. class RforceRetrievalTest < Test::Unit::TestCase
  7.   def setup
  8. @binding = RForce::Binding.new 'https://login.salesforce.com/services/Soap/u/16.0'
  9.     userID = 'yourID' # replace w/ your actual Salesforce developer user ID.
  10.     password = 'password' # replace w/ your Salesforce developer account password.
  11.     securityToken = 'token' # replace w/ your security token
  12.     @binding.login userID, (password + securityToken)
  13.   end
  14.   def test_retrieve_an_account
  15.     answer = @binding.search :searchString => "find {sForce} in name fields returning account(id, phone)"
  16.     record = answer[:searchResponse][:result][:searchRecords][:record]
  17.     assert_equal 'Account', record[:type]
  18.     assert_equal '(415) 901-7000', record[:Phone]
  19.   end
  20. end

Now add the following code in a new ruby project by creating a Test:Unit and run the test to run and retrieve the data from Salesforce.


Read more about documentation on Rforce here


Activesalesforce - How to use 

$ gem install  ActiveSalesforce










Once installed - now open you rail app or create one using this command
$ rail yourapp 
Adding adapter as dependency 

Navigate to app - example if for windows  C:youapp/config/enviorments (folder)

Add this line of code 


#Adding the adapter dependencies for activesalesforce - connecting to salesfore clouds
  require 'activesalesforce' 

For all the three file ( development, test, and production.rb) files

Here we added dependencies, meaning that we specified  we require activesalesforce adapter for our application


Adding salesforce account details in database 

Open up - C:youapp/config/database.yml file 

Add your salesforce account details with this piece of code 


adapter : activesalesforce
username :xxxxxxxx@salesforce.om 
password : XXXXXXXXXXX



Similar to the code show above, try adding salesforce credential and replacing rforce gems by activesalesforce to execute.

I would be performing trials soon and updating the articles with snapshots in here. Read more about Activesalesforce on Douglas blog in here  specially (non-windows users)

Happy Connecting 


You May Also Like

0 comments