Ruby and Salesforce Integration with SOAP

Saturday, January 15, 2011 by Aslam - The Alexendra
Hi All,
I am not a Ruby guy, but last few days i had to look into this language, moreover in the context of calling apex web service method using Ruby. I love doing this using java and php, even i did in python also, but this time its a challenge for me to achieve this. Its a challenge because lack of documentation and sample codes on net and as a newbie in Ruby world. As every new developer i also searched many times on net but was struggling sometimes setting up Ruby, sometimes rubygems, sometimes things were not working. I left many times work in middle long before also. But searching and searching and i found this blogpost by Andrew very helpful. http://hideoustriumph.wordpress.com/2008/05/05/ws-deathstar-for-the-rest-of-us/.

So most of the things i took from that blog only and here i am describing some sample code how i achieve my task.

1) You first need to install ruby. I had InstantRails 2.0. That fits my need.
2) Now first go to your developer account, Setup->Develop->API, choose "enterprise.wsdl" and save it somewhere on your disk , for example in my case i am saving it as below location.
D:\RubyRails\rails_apps\sfdc\enterprise.xml
3) Open the ruby console
4) Then you need to install "SOAP4R" by this command.
gem install soap4r

When i ran this command, i got errors like "Errno::EADDRNOTAVAIL". Its because my gems were outdated, if this is the same case with you then you need to install first updated gems. so...
5) go to this site http://rubygems.org/pages/download
6) Download the gems and unpack to a directory and go to that directory from the command line
7) Install with:
ruby setup.rb

8) Now issue the command to install soap4r
gem install soap4r

Hopefully this time it will get installed.
9) Now go to that directory where you saved the "enterprise.xml" from command line and issue following command.

ruby wsdl2ruby.rb --wsdl D:\RubyRails\rails_apps\sfdc\enterprise.xml --type client --force

You will see 4 files created into your current directory ('sfdc' in above case)

10) Now here is the code for one extra file which i copied from the above blogpost for easy login
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


11) And now here is the final code for doing Login, fetching records from Account object and Printing raw output on screen. Change the username, password and security token as needed.
test.rb
--------------------------------

require 'rubygems'
gem
'soap4r'
require
'soap/soap'
require
'defaultDriver'
require
'client_auth_header_handler'
require
'soap/wsdlDriver'


d
= Soap.new
d.wiredump_dev
= STDOUT

h
= ClientAuthHeaderHandler.new # Create a new handler

l
= d.login(:username => "USERNAME", :password => "PASSWORD" + "SECURITY_TOKEN")

d.endpoint_url
= l.result.serverUrl # Change the endpoint to what login tells us it should be
h.sessionid = l.result.sessionId # Tell the header handler what the session id is
d.headerhandler << h # Add the header handler to the Array of headerhandlers

d.getUserInfo(
"")
d.query(:queryString
=> "select id,name from account")



12) For test the above code, go to ruby console, go to "sfdc" folder and issue following command.
ruby test.rb

13) Now, the main task for which i struggled and not found any easy code on net. Calling apex method from ruby. I tried different things and thought it might be of few lines but may be complex. But finally after so many hit and trial i found that, the code is really very small and easy enough.

14) For example, lets say i want to make one webservice method in Apex to just print one greeting message on screen to passed one Name param. Create one apex class like this.
MyService.cls
--------------------------------
global class MyService{
webservice
static string printMe(string name){
return "Hello " + name;
}
}
15) Save the above class and click on WSDL link/button for this class. You will get one xml file regarding this class. Save it to your disk. For example i am saving this to "service" folder with name "myservice.xml":-
D:\RubyRails\rails_apps\sfdc\service\myservice.xml

16) Now here is the complete code to invoke the "printMe" method, for calling this you also need login first, so the complete example is as below.
test.rb
------------------------------------------------
require 'rubygems'
gem
'soap4r'
require
'soap/soap'
require
'defaultDriver'
require
'client_auth_header_handler'
require
'soap/wsdlDriver'


d
= Soap.new
d.wiredump_dev
= STDOUT

h
= ClientAuthHeaderHandler.new # Create a new handler

l
= d.login(:username => "USERNAME", :password => "PASSWORD" + "SECURITY_TOKEN")

d.endpoint_url
= l.result.serverUrl # Change the endpoint to what login tells us it should be
h.sessionid = l.result.sessionId # Tell the header handler what the session id is
d.headerhandler << h # Add the header handler to the Array of headerhandlers

client
= SOAP::WSDLDriverFactory.new( 'service/myservice.xml' ).create_rpc_driver
client.wiredump_dev
= STDOUT
client.headerhandler
<< h
result
= client.printMe(:name => "Aslam Bari");


17) If all goes well, you will see raw xml output on console printed.
18) Now its time to explore more in your Ruby skills with SFDC :)


Thanks
Aslam Bari

21 comments:

Anonymous said...

Thanks for summarizing this Aslam, I was looking for something like this to get started.

em ubungrammatik said...

you should be looking @ the ASF-SOAP-ADAPTER (aka RubyToolKit). http://asf-soap-adapter.are4.us

vasudha dharani said...

Salesforce Developer Online Training, ONLINE TRAINING – IT SUPPORT – CORPORATE TRAINING http://www.21cssindia.com/courses/salesforce-developer-online-training-144.html The 21st Century Software Solutions of India offers one of the Largest conglomerations of Software Training, IT Support, Corporate Training institute in India - +919000444287 - +917386622889 - Visakhapatnam,Hyderabad Salesforce Developer Online Training, Salesforce Developer Training, Salesforce Developer, Salesforce Developer Online Training| Salesforce Developer Training| Salesforce Developer| If you’re serious about a career in IT, 21st Century would like to provide you a guidance don’t hesitate to organize a free demo session. For any further information regarding the courses once go through our website Visit:http://www.21cssindia.com | Call Us +917386622889 - +919000444287 - contact@21cssindia.com

games unblocked said...

Very interesting! Thanks you
kids games online
friv 2
unblocked games
juegos de un show mas

Mít Trang said...

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this.
juegosjuegos.com
juegos de matar zombies
jogos do friv

for IT the said...

Great Article
Java Online Course | Java EE Training


Java Training Institutes in Chennai | java j2ee training institutes in chennai | Java Training in Chennai | J2EE Training in Chennai | Java Course in Chennai


Java Interview Questions | Java Training Institutes | Core Java Interview Questions

Friv4 Games Online said...

I would like more information about this, because it is very nice., Thanks for sharing.
Signature:
i like play happy wheels demo online and play happy wheels games full and friv , girlsgogames , games2girls

Alice Denny said...

The blog or and best that is extremely useful to keep I can share the ideas. Age Of War 2
Big Farm | Slitherio | Tank Trouble
Of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
Happy Wheels | Goodgeme Empire | Slither.io

Adlearning Technologies said...

Thank You for sharing your article. I like it. We are also giving TIBCO Online Training at affordable prices.

94 hằng said...

Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.

subway surf , baixar subway surf, subway surf download , download subway surf

94 hằng said...

Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also

Square Quick
Square Quick
Square Quick
Square Quick
Square Quick

94 hằng said...

Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.

Snapchat , Snapchat, Snapchat , Snapchat

94 hằng said...

Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also
Download Musicas, Baixar Musicas Gratis ,Baixar Musicas Gratis, Baixar Musicas

94 hằng said...

You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.

entrar hotmail agora , hotmail entrar, entrar hotmail , entrar no hotmail

94 hằng said...

Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.
facebook iniciar sesión , facebook, iniciar sesion , iniciar sesion facebook

94 hằng said...

Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.
dream league soccer download , dream league soccer apk , download dream league soccer , dream league soccer

94 hằng said...

You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.
banana kong ,
banana kong baixar ,
baixar banana kong ,
download banana kong ,
banana kong

94 hằng said...

Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also

whatsapp messenger
baixar whatsapp
whatsapp plus
download whatsapp
whatsapp baixar


94 hằng said...

You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.
Hotmail
Hotmail Iniciar Sesión
Iniciar Sesión
Iniciar Sesión Hotmail
Iniciar Sesión
Iniciar Sesión Hotmail

ngocanh said...

Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here.
b612 l b612 app l baixar b612 l download b612 l b612 apk

ngocanh said...

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. Nice! thank you so much!
geometry dash 2.0 l geometry dash 2.0 apk l geometry dash online l geometry dash 2.0 download l geometry dash

Post a Comment