Heroku Connect: Simplifying Database Synchronization
By Wilson Wang
I am writing this post in hopes of sharing my experience of doing Salesforce integration using Heroku Connect for a Ruby on Rails/React Native app. Hope this is useful for any first-time or interested developers!
What is Heroku Connect?
“Heroku Connect makes it easy for you to build Heroku apps that share data with your Salesforce deployment. Using bi-directional synchronization between Salesforce and Heroku Postgres, Heroku Connect unifies the data in your Postgres database with the contacts, accounts and other custom objects in the Salesforce database.”
Another way to put it…
Heroku Connect assists with replacing your app’s Postgres (PG) database with a Salesforce database. Of course, since Rails apps connect natively to PG, you cannot do immediate calls and pushes to Salesforce without some API like the Restforce gem. In reality, the PG database that our Rails app will interact with serves as a disguise for Salesforce. It is merely a constantly working middleman that all data must go through before reaching Salesforce, and vice versa. Heroku Connect is the bridge that combines the capabilities of Force.com and Heroku without the need for a gem.
You might ask… why even bother learning Salesforce integration?
Well, Salesforce integration streamlines the process of storing and retrieving data, especially customer data for businesses. You can provide your customers with modernized computer systems for an improved user experience and workflow. You’ll be accelerating app development and creating better tools for informing management and sales on business performance. This helps businesses achieve efficient levels of operation for business-to-consumer apps through instantaneous and accurate updates.
A Sample Project for Inspiration
To give some background for the code snippets below in the tutorial, I will explain beforehand the project I was working on that first got me introduced to Heroku Connect.
I’m currently a developer for Blueprint, an organization at UC Berkeley that develops tech solutions for nonprofits and helps them to best reach their goal. This past year, my team worked on building a solution for The Dream Project, whose goal is to provide better education for children in the Dominican Republic.
Previously, Dream recorded student information in a Salesforce database, which was not ideal for teachers to use. To make their lives easier, we created a user-friendly app that handled course creation, student enrollment, and attendance taking. Due to the poor internet connection at Dream campuses, we turned to Heroku Connect for constant synchronization of updates on both the Heroku PG database side and Salesforce database side.
All the code described in this demo is available in our project repo, and you can also check out the React side of the application here.
Alright, let’s start the demo…
Overview
I’m going to tackle this demo in the following order:
- Description of Tech
- Setup (Heroku PG and Heroku Connect)
- Heroku Connect Mappings + Notes
- Changes to your local Rails app
Description of Tech
- Force.com (Salesforce Developer Account)
- Heroku Connect
- Rails app (my Rails version was 5.1.4)
Setting up: Deploying your app to Rails
After setting up your Rails app, you want to deploy your app on Heroku. Here is Heroku’s documentation on deploying your app. Most likely, if this is your first time, you will face an error: “Heroku deployment failed because of sqlite3 gem error”. This is because Heroku uses PG instead of sqlite3. To fix this, check out this Stack Overflow post.
Heroku Postgres
With your app deployed, your overview page should have the add-on Heroku Postgres, which is your usual database for deployed Heroku apps.
Click on this add-on to check the url database info in the settings tab. Be sure to add your Postgres database url to your Rails app.
To add the database url, you can either create an environment variable:
or just include a url variable in your database.yml file:
Heroku Connect
Next, click on configure add-ons or the resources tab, and search for Heroku Connect. Click provision to add the add-on.
You will then be redirected to the screen below. Note that the schema name is “salesforce” for now (feel free to change it as well), and you’re good to continue.
Click setup connection and enter your Salesforce user info to establish the connection with the appropriate details.
You’re now done with the setup! Now we can move on to mappings.
Mappings
I recorded a short video of creating a mapping for a Teacher object: https://www.youtube.com/watch?v=HynwPokOQt4
The video is a walkthrough for establishing mappings, but there are a couple things I want to explain further that I personally got confused with. After reading these clarifications, I would recommend rewatching the video for a better understanding.
1. ‘__c’
You may be asking why there are so many ‘__c’s that are appended at the end of my teacher object and teacher fields. Well, Salesforce actually appends this to each custom class/field. So, tying back to my Rails project, the table name is now Teacher__c instead of Teacher and the columns/fields have changed from First_name, Last_name, etc. to First_name__c, Last_name__c, etc. Some default fields like sfid or createddate are not custom, so they don’t require appending ‘__c’.
2. Custom Fields
So, how do you create custom fields? Well, there’s actually a small arrow on the right side of your screen for you to edit objects and fields.
After each modification, remember to check if you also need to modify your Heroku Connect mappings.
3. Two Way Syncing
I wanted to point out two very cool features about Heroku Connect’s Two-Way Syncing. The first is poll frequency, which regulates how often you want your PG database to update with Salesforce updates. Checking the Accelerate Polling box makes most operations pretty much instantaneous.
The second is the section on pushing data from your PG database to sync with the Salesforce database. You must check the box in order to have this feature work at all. However, right afterward, you will most likely see a warning pop up:
This is a layer of protection to ensure that you don’t accidentally create duplicate objects. It’s great if you have a unique identifier (in the video, each teacher has a unique email, so I made that my identifier). Otherwise, you can skip it and still have a functional Heroku Connect mapping. However, you may want to program your app to double check for existing records before creating an object.
Changes to your local Rails app
Now that you have your Heroku Connect all nice and set up, it’s time for a quick local adjustment before you can interact with your Salesforce data.
In overview, you will see the name of your schema, in this case “salesforce”. If you had picked a different name during the provision phase, that name will show up here instead.
Because my Rails app’s table name is still Teacher, I need to change it to Teacher__c to correctly reference the Salesforce data. I must change the table name to the format schema_name.object_name or salesforce.teacher__c to reflect the changes.
The other notable change applies if your model has validates statements, like my Teacher model. Following the validates keyword, we still have the old field names (first_name without ‘__c’, etc.). We need to change those names by either appending the ‘__c’ to the old field names or by creating separate methods:
Afterwards, create some objects in Salesforce and open up your Rails console to retrieve this info. (In the video, I called Teacher.all to display all Salesforce Teacher objects in my Rails console.) The information should also be reflected in the Heroku Connect explorer.
Reflection
I spent a good half of my semester trying to figure out Salesforce integration through lots of research, experimenting with different solutions, and plenty of support. In the end, it was a very enlightening experience to not only work with the cool features of Salesforce/Heroku/Rails, but also learn how much I’ve grown as a developer and reflect on my strengths and weaknesses. So to all developers out there, no matter what situation you’re in, don’t give up! Don’t be afraid of trying new solutions and failing. Stay positive and Happy Deving!
Wilson Wang is a junior at UC Berkeley studying computer science and data science. He is interested in developing software for improving business-customer relationships. Besides technology, Wilson is passionate about martial arts, architectural design, and landscape photography.