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

The agent-shaped org chart

Every real org has the same topology: principal, role-holder, specialists. Staff AI maps onto it, node for node, and the cost collapse shows up in the deliverables that were always just human-handoff overhead.

AI as staff, not software

Two frames for what AI is doing to work. The tool frame makes tools smarter. The staff frame makes roles unnecessary. Those aren't the same product, the same company, or the same industry.

Knowledge work was never work

Knowledge work was always coordination between humans who couldn't share state directly. The artifacts were never the work. They were the overhead — and AI just made the overhead optional.

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.

How do I get my dev team to adopt AI?

A stub on helping mixed-interest development teams find their own useful ways into AI.

Want to learn about agents? Talk to someone who ran an agency.

I spent 20 years running consulting engagements at Fortune 500 companies. Turns out that's the best preparation for running a fleet of AI agents ... because the problems are identical.

Your AI agents need a water cooler

We run a twelve-session AI fleet that coordinates through an IRC breakroom. A friend asked: why are you making AI agents act like humans? The answer turned out to be more interesting than the question.

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.