Ruby on Rails and Salesforce

How you can integrate Ruby on Rails and Salesforce? This question and more will be resolved in this article. Let’s start! Ruby on Rails (Rails) is an open-source web application framework, written in the Ruby programming language, that is used in many big-name applications, like GitHub, Airbnb, Hulu, Zendesk, and more. And as with all open-source projects, you can always contribute code. 

The Rails Doctrine

Rails has some interesting pillars that every developer should know: 

  1. Optimized for programmer happiness – “Ruby is designed to make programming not only easy but also fun” (Yukihiro Matsumoto)
  2. Convention over Configuration – The environment in which you work assumes many logical situations by default, so if you adapt to them rather than creating your own rules each time, programming becomes an easier and more productive task
  3. Rails is ‘omakase’ – Omakase is when you don’t know what to order in a restaurant and let the chef choose. In Ruby, this is the practice of letting others assemble your stack for you.

Check out the official site of Ruby on Rails and go deep into Ruby.

Integrating Ruby on Rails (Rails) with Salesforce

Salesforce, as the leading CRM, is constantly working to innovate, making it a great platform to integrate your applications with. Rails, like many other platforms, is frontend agnostic, giving you the flexibility to choose the tools and technologies that make the most sense for your project. Salesforce brings a robust set of APIs into the mix, letting you make use of the data and logic in Salesforce within your Ruby application. 

Pick the Right API(s)

This is the one area where you’ll need to make some choices, depending on what you need your application to do with Salesforce. Knowing which APIs you need will help guide which gems to install.

  • SOAP API – Create, retrieve, update or delete records, such as accounts, leads, and custom objects. Allows you to maintain passwords, perform searches, and much more. Optimized for real-time client applications that update a few records at a time. Indeed you can use it for processing many records, but when the data sets contain hundreds of thousands of records, SOAP API is less practical. Process data from a few thousand to millions of records very simple with the Bulk API.
  • REST API – Powerful, convenient, and simple REST-based web services interface for interacting with Salesforce. Based on the same underlying data model and standards objects as the SOAP API. (Other REST APIs that may be of interest: User Interface API, Connect REST API.)
  • Bulk API – This is based on REST principles and is optimized for loading or deleting large sets of data. You can use it to query, queryAll, insert, update, upsert, or delete many records asynchronously by submitting batches. Salesforce processes batches in the background. Also, another benefit of the Bulk API is that it has higher record limits.

Get Authenticated

Salesforce leverages OAuth 2.0 for authentication. There’s no need to code any of this from scratch, just leverage the Ruby gems (ruby software packages) restforce and omniauth-salesforce. Check out connectSFDC for a rails project with omniauth-salesforce installed and ready to interact with a Salesforce Connected app.

Integrate Your Application

If your application needs real-time access and is only updating a few records at a time, you can make use of the Restforce gem to access the REST API.

  1. Add ‘restforce’ or install manually with ‘gem install restforce’)
  2. Instantiate your client object and authenticate using OAuth.
    ‘Client = Restforce.new’
  3. Query away. (See the official Restforce documentation for all your options.)

For the SOAP API, use Soapforce

  1. Add ‘soapforce’ to your Gemfile or install manually with ‘gem install soapforce’.
  2. Instantiate your client object and authenticate using OAuth.
    ‘client = Soapforce::Client.new’
  3. Query as needed. (See the Soapforce Github page for options.)
  4. Log out.
    ‘client.logout’

For the Bulk API, use SalesforceBulkAPI

  1. Add ‘salesforce_bulk_api’ to your Gemfile or install manually with ‘gem install salesforce_bulk_api’
  2. Authenticate using restforce with OAuth.
    ‘client = Restforce.new(…’
  3. Instantiate using the Restforce client.
    ‘salesforce = SalesforceBulkApi::Api.new(client)’
  4. Query as needed. (See the SalesforceBulkAPI page for options.)

Much of the groundwork has been done for you with these gems. The robust Salesforce APIs make it easier to bring the functionality and data into your Rails app. (Not into Ruby? You could always check out our article on Python with Salesforce.) 

Let us know how you’re approaching your Ruby on Rails and Salesforce projects.

Footer - Ruby on Rails and Salesforce