Connecting Ruby on Rails to Salesforce

Salesforce is getting popular with SME and Enterprise markets and companies would love to integrate their custom-built apps with their Salesforce CRM database. You can synchronize data from your database to Salesforce using Salesforce API. Usually organizations are allocated 10,000 API calls per day. If you need more API calls, you can purchase an API calls pack from your Salesforce reseller.

Getting Started

It is a good idea to use a dedicated Salesforce user account to connect to your app and set up appropriate security measures. Databasedotcom gem will parse all fields every time when it connects to Salesforce. If you are using a dedicated user to access the API you can hide the fields that are not in use to reduce bandwidth and sync faster.


Install and configure the gem

Add gem 'databasedotcom' to your Gemfile and then run bundle install.

Enable API Access

  1. Log in to Salesforce
  2. Go to the Remote Access setting
  3. Click New
  4. Fill in your application details, callback URL can be any URL.
  5. Save a copy of your Consumer Key and Consumer Secret

Get a security token

You will need to use your security token to access Salesforce API.

If you don't have your Salesforce security token, go to Reset My Security Token in Salesforce setting and press Reset Security Token. Your new security token should be in your email inbox within a minute.


We will set up one YAML files that will store all Salesforce setting. Create a file called app/config/salesforce.yml and add your Salesforce credentials and Salesforce objects that you want to use in your app. I this example I am using two main objects - Contact and Account. Feel free to add more objects to the list.

    client_id: copy and paste your Salesforce Consumer Key
    client_secret: copy and paste your Salesforce Consumer Secret
    password: password
    security_token: Jdkra7ZNpJcYEmOaJPvPqBJRW
    objects: [Contact, Account]

Now create a Salesforce class in lib/Salesfoce.rb file that will initialize Salesforce connection, authenticate your app and cache your Salesforce objects schema.

Create an initializer file config/initializers/sfdc_init.rb that will authenticate Salesforce when your app starts:

Usage Examples

Now when you start your Rails app or console, you have access to your Salesforce database. Some usage examples: