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:-
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=”http://soap.sforce.com/schemas/class/WebToLeadServices” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns=”http://schemas.xmlsoap.org/wsdl/” xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/” xmlns:tns=”http://soap.sforce.com/schemas/class/WebToLeadServices”>
<types>
<xsd:schema elementFormDefault=”qualified” targetNamespace=”http://soap.sforce.com/schemas/class/WebToLeadServices”>
.
.
.
<xsd:complexType name=”WebLead”>
<xsd:sequence>
<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:sequence>
</xsd:complexType><xsd:element name=”createLeadFromWeb”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”wl” type=”tns:WebLead” nillable=”true”/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
.
.
.
<service name=”WebToLeadServicesService”>
<documentation></documentation>
<port binding=”tns:WebToLeadServicesBinding” name=”WebToLeadServices”>
<soap:address location=”https://ap7.salesforce.com/services/Soap/class/WebToLeadServices”/>
</port>
</service>
</definitions>
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.
gem install soap4r
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):
client_auth_header_handler.rb
require ‘soap/header/simplehandler’
class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
SessionHeader = XSD::QName.new(“rn:enterprise.soap.sforce.com”, “SessionHeader”)
attr_accessor :sessionid
def initialize
super(SessionHeader)
@sessionid = nil
end
def on_simple_outbound
if @sessionid
{“sessionId” => @sessionid}
end
end
def on_simple_inbound(my_header, mustunderstand)
@sessionid = my_header[“sessionid”]
end
end
install these two gems
gem ‘soap4r’
gem ‘soap4r-spox’
lib/salesforce/default.rb
lib/salesforce/defaultDriver.rb
lib/salesforce/defaultMappingRegistry.rb
lib/salesforce/SforceServiceClient.rb
lib/salesforce/client_auth_header_handler.rb
and change require path accordingly on each files e.g.
require ‘salesforce/default.rb’
require ‘salesforce/defaultMappingRegistry.rb’
require ‘soap/rpc/driver’
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 = Soap.new
d.wiredump_dev = STDOUT
h = ClientAuthHeaderHandler.new
username = “yourusername”
password = “yourpassword”
token = “yoursectoken”
l=d.login(:username => username, :password => password+token)
h.sessionid = l.result.sessionId
d.headerhandler << h
client = SOAP::WSDLDriverFactory.new( ‘lib/salesforce/webtosalesforcelead.xml’ ).create_rpc_driver
client.wiredump_dev = STDOUT
client.headerhandler << h
leadData = {}
leadData[“wl”] = sf #=> “wl” createLeadFromWeb element
Rails.logger.info “========#{leadData.inspect}============”
response = client.createLeadFromWeb(leadData) #=> createLeadFromWeb method which is present in webtosalesforcelead.xml
#Rails.logger.info “========#{leadData.inspect}============”
Rails.logger.info “========#{response.inspect}============”
#response = client.WebLead(sf)
end
end
That's all for steps to integrate Salesforce REST API with Ruby on Rails. If you have any query, please write in comment section.
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=”http://soap.sforce.com/schemas/class/WebToLeadServices” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns=”http://schemas.xmlsoap.org/wsdl/” xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/” xmlns:tns=”http://soap.sforce.com/schemas/class/WebToLeadServices”>
<types>
<xsd:schema elementFormDefault=”qualified” targetNamespace=”http://soap.sforce.com/schemas/class/WebToLeadServices”>
.
.
.
<xsd:complexType name=”WebLead”>
<xsd:sequence>
<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:sequence>
</xsd:complexType><xsd:element name=”createLeadFromWeb”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”wl” type=”tns:WebLead” nillable=”true”/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
.
.
.
<service name=”WebToLeadServicesService”>
<documentation></documentation>
<port binding=”tns:WebToLeadServicesBinding” name=”WebToLeadServices”>
<soap:address location=”https://ap7.salesforce.com/services/Soap/class/WebToLeadServices”/>
</port>
</service>
</definitions>
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):
client_auth_header_handler.rb
require ‘soap/header/simplehandler’
class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
SessionHeader = XSD::QName.new(“rn:enterprise.soap.sforce.com”, “SessionHeader”)
attr_accessor :sessionid
def initialize
super(SessionHeader)
@sessionid = nil
end
def on_simple_outbound
if @sessionid
{“sessionId” => @sessionid}
end
end
def on_simple_inbound(my_header, mustunderstand)
@sessionid = my_header[“sessionid”]
end
end
(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.
lib/salesforcelib/salesforce/default.rb
lib/salesforce/defaultDriver.rb
lib/salesforce/defaultMappingRegistry.rb
lib/salesforce/SforceServiceClient.rb
lib/salesforce/client_auth_header_handler.rb
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 = Soap.new
d.wiredump_dev = STDOUT
h = ClientAuthHeaderHandler.new
username = “yourusername”
password = “yourpassword”
token = “yoursectoken”
l=d.login(:username => username, :password => password+token)
h.sessionid = l.result.sessionId
d.headerhandler << h
client = SOAP::WSDLDriverFactory.new( ‘lib/salesforce/webtosalesforcelead.xml’ ).create_rpc_driver
client.wiredump_dev = STDOUT
client.headerhandler << h
leadData = {}
leadData[“wl”] = sf #=> “wl” createLeadFromWeb element
Rails.logger.info “========#{leadData.inspect}============”
response = client.createLeadFromWeb(leadData) #=> createLeadFromWeb method which is present in webtosalesforcelead.xml
#Rails.logger.info “========#{leadData.inspect}============”
Rails.logger.info “========#{response.inspect}============”
#response = client.WebLead(sf)
end
end
That's all for steps to integrate Salesforce REST API with Ruby on Rails. If you have any query, please write in comment section.
No comments:
Post a Comment