Salesforce API integration with Rails App.

Salesforce is known to be a Customer Relationship Management (CRM) platform and it provides cloud-based applications for Sales, Service & Marketing. This article intends to develop a clearer understanding of the Salesforce API and how you can sync data between Salesforce and your Rails application.

The following steps are required to be followed to integrate Salesforce API with your Rails application:

Use Rails gem “restforce” —


Add gem ‘restforce’, ‘~> 2.5.3’ into your Gemfile
Run ‘bundle install’ or ‘gem install restforce’


In your application.yml, set the following env variables –

SALESFORCE_HOST: “host domain name”

Try to connect to salesforce run “sf =”

Querying to Salesforce

Salesforce can have multiple objects like- Account, Certification_c, Learner etc. To access these objects, you can apply the following queries.

  • query
  • query_all
  • select
  • search
  • create
  • find
  • update
  • destroy

1. query

Example 1:

accounts = sf.query(“select Id, Something__c from Account”)

Example 2: Time based query using WHERE clause
# last synced time

last_synced_at = ( — 1.hour).strftime(“%Y-%m-%dT%H:%M:%SZ”)

#current system time

current_time =“%Y-%m-%dT%H:%M:%SZ”)

sf.query(“select Id, Something__c from Account WHERE LastModifiedDate >= #{last_synced_at} AND LastModifiedDate <= #{current_time}”)

2. query_all

query_all lets you to include the results from your query that Salesforce hides in the default “query” method. These also include soft-deleted records and archived records (e.g. Task and Event records, usually archived automatically after they get a year old)

accounts = sf.query_all(“select Id, Something__c from Account where isDeleted = true”)

3. select

select allows the fetching of a specific list of fields from a single object. It requires an external_id lookup but is often much faster than an arbitrary query.‘Account’, ‘001D000000INjVe’, [“Id”], ‘Some_External_Id_Field__c’)

4. Search

# Find all occurrences of ‘bar’‘FIND {bar}’)

5. create

# Add a new Certification_c

sf.create(‘Certification_c’, Certification_type_c: ‘CSR In-Person’)

6. find

# Find a certification by id

sf.find(‘Certification_c’, ‘001D000000INjVe’)

7. update

# Update the ‘Certification_c’ with `Id` ‘0016000000MRatd’

sf.update(‘Certification_c’, Id: ‘0016000000MRatd’, Certification_type_c: ‘CSR Vertual’)

8. destroy

# Destroy a certification by id

sf.destroy(‘Certification_c’, ‘001D000000INjVe’)

API Limits

Salesforce has a limit of API call per/day/hour. So, you can simply check your Total API calls and available calls.

limits = sf.limits

Output = {“Max”=>15000, “Remaining”=>14746}

Rescue from Restforce API connection errors

def self.connect_to_salesforce
sf = nil
sf =
rescue Exception => e
error_message = "Failed to connect to Salesforce using restforce gem!"




Ruby on Rails Development Company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS371p Fall 2020: Pranay Kalagara

Low self-esteem software developer’s life-g0ing-style.

How to Get an Awesome Graphical Analysis from Sysstat Metrics?

How to Fix “Cronjobs Not Running” issue in Webuzo Control Panel

Why Flutter is the First Choice for Startup Mobile App Development?

The Beginnings of the Rensselaer Center for Open Source

Exceptions vs. Returns

How I built my shipping container house — the hâB

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Ruby on Rails Development Company

More from Medium

Custom Lenses for the AWS Well-Architected Tool

API Usage Plans using AWS API Gateway

AWS Instances and Datadog: How to Save Your money

Migration Of On-Prem PCI Stack to AWS Reference Architecture