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

A Developer’s Opinion on Salesforce

What is the Main Problem We Faced?

How to Connect Rails to Salesforce

Setting Up in Salesforce

Setting up a Rails app

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:

--

--

--

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

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

Recommended from Medium

Effective Cable Management Planning In Modern Data Center Architecture

Loading Assets in Phaser 3

Non-Tech But Still A Techie✨

My Honest Review About Scaler Academy

Design Trade-offs

Fixing Gmail’s dark mode issues with CSS Blend Modes

Introducing Imgup Augtools

How to host an WordPress on AWS in 5 mins.

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

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

More from Medium

Preparing OutSystems Front-end Developer Specialist exam

How to rescue multiple exception classes with Ruby Exception

Bite-size JavaScript Functions for OutSystems Development — Part 1

A practical guide to Visual Regression Testing with Cypress