Many companies use Salesforce, it’s one of the biggest Customer Relationship Managers (CRMs) available right now. Many companies use Rails as well. Combining the power of Rails with the power of Salesforce can greatly benefit any company. Where do you begin with this process? Let us look into a (not so little) Ruby gem, named Restforce.

Restforce

 

Restforce is a Ruby gem made for the Salesforce REST API. It includes many features that help developers interact with the Salesforce API using REST, and has been downloaded over 9,000,000 times. Installation is simple, just like any Ruby gem, just write

gem ‘restforce’

Into your Gemfile, and then

bundle install

Into your terminal.

Initialization

 

Before you begin sending requests to your Salesforce org, you’ll need to first initialize and authenticate your client inside a model as a method, so you can connect properly. There are many ways to do this, but here, I am going to use the username/password authentication method.

Here we are setting the client. There are many variables involved to get a proper connection, so I will go over each of them.

  • Username and password are, of course, the account’s username and password.
  • The security token you can get by requesting one from Salesforce. In order to see your token, you must go to SETUP > MY PERSONAL INFORMATION > RESET MY SECURITY TOKEN. From here you can simply press the button to get an email from Salesforce, giving you a new security token. Note, that the only way to view your security token, is to change it. So save the token after receiving it, in order to avoid hassle later. Changing the security token will also lead Restforce to return authentication errors until the security token is updated in the codebase or environment variables.
  • To view your client ID and your client secret ID, you must go to SETUP > APPS > CONNECTED APPS > MANAGE CONNECTED APPS. From there you should see a list of apps you have connected. Click on one to see all its information, including the client ID and the client secret ID located in the OAuth section.
  • API version is exactly what is sounds like. It is normally best practice to make this the latest version of the Salesforce API.

It is also a good practice to add the keys, passwords, etc into environment variables. Having access to these keys means you have access to any records and fields in Salesforce! Now that everything is set, let’s begin interacting with the API.

Requests

 

Requests are the main way to interact with any REST API. Restforce makes it incredibly easy to make any request to the Salesforce API.

  • As any software developer knows, GET requests are when you need to pull data from an API. This can be done a number of ways through Restforce. Here are a few of the most common ways to get data.

  • POST requests are the next most important. These let you send information to the Salesforce API.

  • PATCH requests are like POST requests, in that you send information to the Salesforce API. However, this time you are changing an object, rather than creating a new one.

  • Finally DELETE requests will remove objects from the Salesforce API.

With these methods at your disposal, you are ready to integrate the power of the Salesforce API into your Rails project.

Example

 

Model

Controller

You can find the Github repository here.

API Call Limit

 

Note that each Restforce call you make is an API call, each Salesforce account is given a certain amount of API calls per month (depending on your Salesforce contract). Be sure to check your API usage. If you’re interested in updating a massive number of records, then perhaps use dataloader.io or the application “wizard”, where you can upload a csv to massively create, delete, and update records. You can read more about that on our blog here.

Data Sync

 

While being able to connect your Rails application to your organization’s Salesforce account can be an empowering tool, there is the risk having data sync issues if you’re storing records in your own database in addition to creating and updating records in Salesforce. It’s important to note that when you create Salesforce records, the record could change in Salesforce, and your Rails application won’t even know about it. So when using this gem, one thing to think about is where your source of truth is if you’re saving and updating in multiple places. Saving to your own database is still a good practice in case for some reason there is an outage out of your control on the Salesforce side of things.

Conclusion

 

Now you know how to integrate your Salesforce org and your Rails app. Using the Restforce gem, you can easily connect to your org. Making REST requests is also extremely simple. Simple methods allow easy GET, POST, PATCH, and DELETE requests to be made. Now that Rails and Salesforce are connected, interacting with Salesforce through your own app has never been easier.

Join the discussion One Comment

  • […] If you are looking for a flexible and cheap way to create a CRM, then building your application from scratch would be your best choice! Especially if you are looking to create a much more personalized CRM that works your way. On the other hand, if you are looking for something simple and streamlined, Salesforce is the choice for you! As anyone can learn Salesforce! In the end, it’s whatever benefits your business the most, for whatever its needs may be. You could even do both, with the ability to integrate the Salesforce API into your Rails project. […]

Leave a Reply