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

17 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

21cssIndia 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

Unknown said...

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

Unknown 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

Unknown 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

Unknown said...

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

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

Kamalakar said...

very usefull informatation.and iam looking for more posts like this please keep updating us........

Unknown said...

Best Technology share blog regarding Ruby and Sales force Integration with SOAP(Simple Object Access Protocol) I will share this blog information to some of my friends who are willing to know on sales force and also I am a PMP Certified person I have done my PMP in bangalore This helps me to know more about the different elements Thankyou

Unknown said...

One of the finest blog which i have seen in the recent days I am having enough interest in regards to specific set of Salesforce projects in which i will be able to access certain projects As I completed my PMP Course in Chennai, I will undertake some projects which will examine for the development of the Salesforce in regards to PMP. Thank you for providing the information

DIAC said...

Learn Salesforce.com developer courses online/classroom in Delhi from top training institutes and get Salesforcedeveloper certification. Get details on course fees @91-931OO96831!!

Doorstephub said...

really good piece of information, Thanks for sharing more information.
Best Salesforce Training Institute in Hyderabad
Salesforce Admin Training in Hyderabad
Salesforce Developer Training in Hyderabad
Salesforce Lightning Training in Hyderabad

ciitnoida said...

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.

Best Linux training in Noida
Linux Training Institute in Noida
Shell Scripting Training Institute in Noida

anosh said...


تعد شركة تركيب اثاث ايكيا بالرياض هي الشركة الرائده والاولي في كافة الاثاث من تركيب وفك ونقل وتخزين وكافة الاعمال المتعلقة بالاثاث في الرياض وكافة المناطق والمحافظات بالمملكة العربية السعودية، وقد تصدرت شركة خبراء المملكة لتكون الأولى في مجال فك ونقل وتركيب الأثاث المنزلي وايضا فك وتركيب الستائر بالرياض وهي تتميز عن باقي شركات الرياض نظرا لما تقدمة من خدمات بشكل احترافي كما انها تتميز عن غيرها بكفاءة الفنيين والامتخصصين في مجال تركيب الاثاث فلا داعي لكثرة البحث فلديك خبراء المملكة فهم فعلا خبراء ومتميزون في جميع خدماتهم المقدمة
شركة تركيب اثاث ايكيا بالرياض
فني تركيب اثاث ايكيا بالرياض
شركة تركيب ستائر بالرياض
عامل تركيب ستائر بالرياض
شركة تركيب غرف نوم بالرياض
فني تركيب غرف نوم بالرياض
شركة تركيب باركية بالرياض
شركة تركيب عفش بالرياض
ما يميز شركة تركيب نقل وتركيب اثاث بالرياض
- تعد شركة تركيب اثاث ايكيا من الشركات المفضلة لكثير من العملاء فهم من منحوها الصدارة والتميز لتميز الخدمات المقدمة لهم وهي الاولي في تركيب الاثاث لزيادة خبراتها الكبيرة لسنوات.
تتميز ايضا شركة خبراء المملكة بكبر فرق العمل المتخصصة والمدربة بمهاره وتقنية عالية كما اننا ندعم صفوفنا بصفة مستمرة من العمال والموظفين والفنيين الأكفاء والمهرة كما انها Jستقبل العمالة الفليبنية

Unknown said...

Really a good post, thanks for sharing .keep it up.

Best Web Design Training Institutes in Noida

Best Hadoop Training Institutes In Noida

Best Digital Marketing Training Institute in Noida

Sap Training Institute in Noida

Best Java Training Institute in Noida

SAP SD Training Institute in Noida

Best Auto CAD Training Institute In Noida

nancy said...

This is a 2 good post. This post gives truly quality information.



RPA Training in Hyderabad

Post a Comment