Skip to main content
Paul Welty, PhD AI, WORK, AND STAYING HUMAN

· ruby-on-rails · 3 min read

Rails + sugarcrm + soap - get a list of sugar accounts into Rails as an array

Integrate SugarCRM with Rails using SOAP to effortlessly sync company names and streamline your project management workflow.

I have long wanted to make sure my CRM system (SugarCRM) and my project management system synchonized certain data, mainly company names. I hate having to sync stuff like that manually. So, I’ve been working on integrating the data using a SOAP client on the Rails side. It took all day to get this working. I don’t know why this took forever to find out, or why there aren’t many good references on the Web. (Maybe I’m just dense!)

I had to patch together a bunch of code, re-code some PHP examples, and do quite a bit of experimenting, to get this all working. Here is a list of some of the sites I used:

http://www.sugarcrm.com/forums/showthread.php?t=17954&highlight=get_entry_list

Good PHP examples about how certain SOAP calls are structured.

http://www.ruby-doc.org/stdlib/libdoc/soap/rdoc/index.html

Basics on how the SOAP objects work. Nothing really useful here, but it helped a little.

http://www.beanizer.org/site/content/view/2/29/lang,en/

Another good PHP reference.

http://kousenit.wordpress.com/2006/08/08/ruby-web-service-clients/

Has some general SOAP info, but not on SugarCRM

http://www.sugarcrm.com/wiki/index.php?title=SOAP_in_RUBY

Sugar’s own starter directions. This is the backbone of the code below.

http://www.sugarcrm.com/wiki/index.php?title=SOAP_Documentation

Sugar’s SOAP documentation. I have to admit that this doesn’t make a lot of sense to me.

http://martyhaught.com/articles/2006/08/04/mapping-soap-response-without-wsdl/

Good example of how confusing this can be. Note that Marty’s situation is exactly where I got stuck. It turns out that these “special” attributes in the SOAP Mapping Object response (__xmlattr, __xmele, etc.) are not really important to the programmer. You can access the data with special attributes that refer to the attributes returned from the server. At least that’s my theory. So, in my example below, you access the results you want using the result.entry_list attribute, because the “get_entry_list” command returns that variable. Note that when you examine the results object, you don’t see these special attributes. I’m sure this is some special Ruby thing that I don’t understand.

Anyway, here’s the code for getting a list of all companies, called “Accounts” in SugarCRM, into Rails in an array.

 def list_accounts
    require 'soap/wsdlDriver'
    require 'digest/md5'
    u = "username"
    p = Digest::MD5.hexdigest("password")
    ua = {"user_name" => u,"password" => p}
    wsdl = "http://your-sugar-web-site.com/soap.php?wsdl"

    #create soap
    s = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

    #uncomment this line for debugging. saves xml packets to files
    #s.wiredump_file_base = "soapresult"

    #create session
    ss = s.login(ua,nil)

    #check for login errors
    if ss.error.number.to_i != 0 

    	#status message
    	logger.debug "failed to login - #{ss.error.description}"

    	#exit program
    	exit

    else

    	#get id
    	sid = ss['id']

    	#get current user id
    	uid = s.get_user_id(sid)

    	#status message
    	logger.debug "logged in to session #{sid} as #{u} (#{uid})"

         #the part below is general. you can use it to get any type of data you want. just change the "module_name"

         module_name = "Accounts"

         query = "" # gets all the acounts, you can also use SQL like "accounts.name like '%company%'"
         order_by = "" # in default order. you can also use SQL like "accounts.name"
         offset = 0 # I guess this is like the SQL offset
         select_fields = ['name','industry'] # this can't be an empty array, my testing showed
         max_results = "1000000" # if set to 0 or "", this doesn't return all the results, like you'd expect
         deleted = 0 # whether you want to retrieve deleted records, too

         result = s.get_entry_list(sid,module_name,query,order_by,offset,select_fields,max_results,deleted)

         #below is where we build the array of names. note that everything gets returns in a name-value pairs hash (name_value_list), using the field list from the request

         @output = []
         for entry in result.entry_list
            item = {}
            for name_value in entry.name_value_list
              item[name_value.name]=name_value.value
            end
           @output << item
         end
        
    	#logout
    	s.logout(sid)

    	#status message
    	logger.debug "logged out"
    	    	
    end

Why customer tools are organized wrong

This article reveals a fundamental flaw in how customer support tools are designed—organizing by interaction type instead of by customer—and explains why this fragmentation wastes time and obscures the full picture you need to help users effectively.

Infrastructure shapes thought

The tools you build determine what kinds of thinking become possible. On infrastructure, friction, and building deliberately for thought rather than just throughput.

Server-side dashboard architecture: Why moving data fetching off the browser changes everything

How choosing server-side rendering solved security, CORS, and credential management problems I didn't know I had.

The work of being available now

A book on AI, judgment, and staying human at work.

The practice of work in progress

Practical essays on how work actually gets done.

The inbox nobody reads is the one that matters

Every organization has a monitoring system that works perfectly and reports to nobody. The gap between having information and acting on it is where most failures actually live.

The best customers are the first ones you turn against

Every subscription makes a bet that most customers won't use what they're paying for. The customer who closes that gap becomes a problem to be managed.

Delegation without comprehension is just prayer

The organizations that survive won't be the ones that automated the most. They'll be the ones that figured out what to stop delegating.

Rails + sugarcrm - an alternative approach

Discover a faster, simpler way to connect Rails with SugarCRM by accessing the database directly, avoiding slow API calls for seamless synchronization.

Rails: Parsing ical calendar on a password-protected webdav server

Learn how to parse iCal calendars from a password-protected WebDAV server using Ruby on Rails and the iCalendar plugin for seamless integration.

Nested resources in Ruby on Rails: Why bother?

Discover the pros and cons of using nested resources in Ruby on Rails and learn when they truly add value to your application development.