Saturday 9 December 2017

How to integrate Salesforce webservices with Ruby on Rails

In this article we will see how to integrate Salesforce REST API to push data from Ruby on Rails application. Please note it requires Ruby greater than 2.0 because of TLS security issues. 

Here are the detailed steps for integrating Salesforce API with Rails application:-

(a) GET WSDL Files and Security Token

Enterprise/ParterWSDL Files: If you have salesforce account you can get your WSDL files from there. You can get this file from your salesforce account “App Setup > Develop > Download your organization-specific WSDL” link.

<?xml version=”1.0″ encoding=”UTF-8″?>
Web Services API : WebToLeadServices
<definitions targetNamespace=”” xmlns:xsd=”” xmlns=”” xmlns:soap=”” xmlns:tns=””>
<xsd:schema elementFormDefault=”qualified” targetNamespace=””>
<xsd:complexType name=”WebLead”>
<xsd:element name=”name” minOccurs=”0″ type=”xsd:string” nillable=”true”/>
<xsd:element name=”email” minOccurs=”0″ type=”xsd:string” nillable=”true”/>
<xsd:element name=”phone” minOccurs=”0″ type=”xsd:string” nillable=”true”/>
<xsd:element name=”comments” minOccurs=”0″ type=”xsd:string” nillable=”true”/>
</xsd:complexType><xsd:element name=”createLeadFromWeb”>
<xsd:element name=”wl” type=”tns:WebLead” nillable=”true”/>
<service name=”WebToLeadServicesService”>
<port binding=”tns:WebToLeadServicesBinding” name=”WebToLeadServices”>
<soap:address location=””/>

Webtolead.wsdl : If you want to send data from your website to salesforce then it will require wsdl files for e.g webtosalesforcelead.xml with content something like this.

Security token : Navigate to My Settings > Personal > Reset My Security Token and click on “Reset Security Token”. This will send you security token on your registered mail id.

(b) Install the SOAP4R gem using Terminal.

gem install soap4r

(c) Run wsdl2ruby

Go to folder where have install soam gem. run the following commands in terminal.

cd /path/to/gem/soap/
wsdl2ruby.rb –wsdl ~/path/to/enterprise.wsdl.xml –type client

Change your paths accordingly. This will produce 4 files in your current working directory: default.rb, defaultDriver.rb, defaultMappingRegistry.rb, and SforceServiceClient.rb.

One more file is required as SOAP4R uses “header handlers” to modify outgoing and read incoming headers (and note that these are SOAP headers, not HTTP headers), so you’ll have to write one of your own (syntax highlighted version):


require ‘soap/header/simplehandler’
class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
SessionHeader =“”, “SessionHeader”)

attr_accessor :sessionid
def initialize
@sessionid = nil

def on_simple_outbound

if @sessionid
{“sessionId” => @sessionid}

def on_simple_inbound(my_header, mustunderstand)

@sessionid = my_header[“sessionid”]

(d) Create Rails application

install these two gems

gem ‘soap4r’
gem ‘soap4r-spox’

(e) Put all generated files and client_auth_header_handler.rb in you salesforce folder in lib folder of your project.


and change require path accordingly on each files e.g.

require ‘salesforce/default.rb’
require ‘salesforce/defaultMappingRegistry.rb’
require ‘soap/rpc/driver’

(f) Create helper salesfore_helper.rb

module SiteHelper
require ‘rubygems’
gem ‘soap4r’
require ‘soap/soap’
require ‘salesforce/defaultDriver’
require ‘salesforce/client_auth_header_handler’
require ‘soap/wsdlDriver’

def sendtosf

sf = User.first
d =
d.wiredump_dev = STDOUT
h =
username = “yourusername”
password = “yourpassword”
token = “yoursectoken”
l=d.login(:username => username, :password => password+token)
h.sessionid = l.result.sessionId
d.headerhandler << h
client = ‘lib/salesforce/webtosalesforcelead.xml’ ).create_rpc_driver
client.wiredump_dev = STDOUT
client.headerhandler << h
leadData = {}
leadData[“wl”] = sf #=> “wl” createLeadFromWeb element “========#{leadData.inspect}============”
response = client.createLeadFromWeb(leadData) #=> createLeadFromWeb method which is present in webtosalesforcelead.xml “========#{leadData.inspect}============” “========#{response.inspect}============”
#response = client.WebLead(sf)

That's all for steps to integrate Salesforce REST API with Ruby on Rails. If you have any query, please write in comment section.