Bulk API: Process large sets of records from Rails app to Salesforce

A Developer’s Opinion on Salesforce

Salesforce is a cloud-based customer relationship management platform that brings companies and customers together. Salesforce offers many useful functions in addition to CRM. And Salesforce leaves room for developers to exercise creativity, including write your own applications and integrating modern web frameworks into the platform. Plenty of interesting stuff.

What is the Main Problem We Faced?

The central issue of our project was to grab data from Salesforce, process the data and then update Salesforce with the new or modified data. To upload data to Salesforce, we choose Bulk API. Bulk API is optimized to load or delete many records asynchronously. It’s faster than SOAP-based API due to parallel processing and fewer network roundtrips. Bulk API allows for batch sizes up to 10,000 records and gives the opportunity to avoid the standard limits of 200 records. But the first issue we faced was when we tried to connect the Rails App to Salesforce.

How to Connect Rails to Salesforce

To connect your app to Salesforce Online, the first thing you need to do is go to https://developer.salesforce.com/signup and create a developer account. This is how you obtain a client ID and client secret, which you will need to securely connect your Rails application to your salesforce account.

Setting Up in Salesforce

Once you log in to Developer Salesforce, press “Setup” in the dropdown Settings menu as shown on the screenshot.

Setting up a Rails app

Step 1. Add the gems below and run bundle install

gem 'restforce'
gem 'omniauth-salesforce'
gem 'salesforce_bulk_api'
OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
provider :salesforce, YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, {
callback_url: '/auth/salesforce/callback'
}
end
Rails.application.routes.draw do
match 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post]
match 'auth/failure', to: redirect('/'), via: [:get, :post]
match 'signout', to: 'sessions#destroy', as: 'signout', via: [:get, :post]
end
rails g model User
class User < ApplicationRecord
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid).permit!).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.refresh_token = auth.credentials.refresh_token
user.instance_url = auth.credentials.instance_url
user.save!
end
end
end
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :provider
t.string :uid
t.string :name
t.string :oauth_token
t.string :refresh_token
t.string :instance_url

t.timestamps
end
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

private

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

helper_method :current_user
end
class SessionsController < ApplicationController
def create
user = User.from_omniauth(request.env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end

def destroy
session[:user_id] = nil
redirect_to root_url
end
end
class WelcomeController < ApplicationController
def index
if current_user
client = Restforce.new :oauth_token => current_user.oauth_token,
:refresh_token => current_user.refresh_token,
:instance_url => current_user.instance_url,
:client_id => YOUR_CONSUMER_KEY,
:client_secret => YOUR_CONSUMER_SECRET

salesforce = SalesforceBulkApi::Api.new(client)

records = [{name: "Test Account", type: "Other"}]
# You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
salesforce.create("Account", records)

@accounts = client.query("select Id, Name from Account")
end
end
end
root 'welcome#index'
<%= link_to "Sign in with Salesforce", "/auth/salesforce", id: "sign_in" %>

Useful links:

--

--

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
Active Bridge

Active Bridge

26 Followers

Ruby on Rails development house. We assist businesses in building products that people enjoy. Share knowledge about #RoR #Web #CloudSoftware #ProductDevelopment