Using the Partner Salesforce web service from Coldfusion to return data

Getting data from the Partner Salesforce web service via Coldfusion.

My last article on the subject of connecting and using Salesforce.com web service from ColdFusion has garnered a fair amount of emails asking for help, so I’d thought I’d write a short follow up.

The following is a very simple script that I compiled using a lot of older partial examples and documentation over at the DeveloperForce. The Coldfusion documentation and examples at DeveloperForce are lacking, but because there are so many Java based examples one can extrapolate them to ColdFusion.

<CFAPPLICATION NAME="sdfasdfawe534534534jklsdf" APPLICATIONTIMEOUT="#CreateTimeSpan(0, 2, 0, 0)#" CLIENTMANAGEMENT="NO" SESSIONMANAGEMENT="NO" SETCLIENTCOOKIES="NO" SETDOMAINCOOKIES="NO"> <CFSET Application.isError = "false" /> <!--- Only one session should login at a time. ---> <CFLOCK timeout="10" scope="Application" throwOnTimeout="no" type="exclusive"> <!--- The SFDC login session should remain active for 30 minutes ---> <CFIF NOT IsDefined("Application.sfdc") OR Application.sfdc EQ "" OR NOT IsDefined("Application.lastLogin") OR (IsDefined("Application.lastLogin") AND Abs(DateDiff("n", Application.lastLogin, Now())) GT 30)> <CFSET Application.sfdc = createObject("webservice","http://some.url/your-partner-file.wsdl") /> <CFSET loginResult = Application.sfdc.login("username","password") /> <!--- Create the SOAP Header that will contain the Session ID ---> <CFSET authHeader=createObject("java","org.apache.axis.message.SOAPHeaderElement").init("SforceService", "SessionHeader") /> <CFSET Application.sfdc.setHeader(authHeader) /> <!--- Add (and populate) a text node called sessionId: ---> <CFSET authHeader.addChildElement("sessionId").addTextNode(loginResult.getSessionId()) /> <!--- Change the endpoint URL to what was returned by the login method: ---> <cfset Application.sfdc._setProperty("javax.xml.rpc.service.endpoint.address",loginResult.getServerURL()) /> <CFSET Application.lastLogin=Now() /> </CFIF> </CFLOCK> <CFTRY> <!--- Only one session at a time should call this method ---> <CFLOCK timeout="10" scope="Application" throwOnTimeout="no" type="readOnly"> <!--- JDR: this gets the current user you used to login to the webservice ---> <cfset rssfResponse1 = Application.sfdc.getUserInfo() /> <!--- JDR: this gets some contacts from Salesforce ---> <cfset rssfResponse2 = Application.sfdc.query("SELECT FirstName, LastName FROM Contact LIMIT 11") /> </CFLOCK> <cfoutput> <## >Current User Information</## > <div>getOrganizationName() | <strong>#rssfResponse1.getOrganizationName()#</strong></div> <div>getUserEmail() | <strong>#rssfResponse1.getUserEmail()#</strong></div> <div>getUserId() | <strong>#rssfResponse1.getUserId()#</strong></div> <div>getUserFullName() | <strong>#rssfResponse1.getUserFullName()#</strong></div> <## >Get 10 Contact records:</## > <ol> <!--- JDR: get the array length ---> <cfset getLenReturn = arraylen(rssfResponse2.getRecords()) - 1> <!--- JDR: loop over the returned reston ---> <cfloop from="1" to="#getLenReturn#" index="i"> <li>#rssfResponse2.getRecords(i).get_any()[1].getValue()# #rssfResponse2.getRecords(i).get_any()[2].getValue()#</li> </cfloop> </ol> </cfoutput> <!--- Catch any errors ---> <CFCATCH type="ANY"> <!--- Reset the sfdc session ---> <CFSET Application.sfdc = "" /> <CFIF NOT IsDefined("Application.isError") OR Application.isError NEQ "true"> <h1>An error occurred</h1> <h2>Error Details</h2> <CFDUMP VAR="#CFCATCH#" /> <CFSET Application.isError = "true" /> </CFIF> </CFCATCH> </CFTRY>

Let’s break this down a bit.

  1. The first cflock block setups up the initial session and connection to Salesforce. Key things to take away from this block include that you’ll need to generate your Partner WSDL from Salesforce (see documentation) and put it somewhere you’ll have access to it (you’ll need to host it at the very least on your local web server for this to work). Once you change the username and password, the rest is done for you. You won’t need to edit anything else in this block.

  2. Once we have our connection ready, we can call the services the Partner WSDL offers. In this example, setup two calls, rssfResponse1 and rssfResponse2.

    • rssfResponse1 calls getUserInfo(), which returns information about the user you used to create the login to the webservice *rssfResponse2 calls query(), which using SOQL queries our contacts, limiting the result to 11 rows. 3.After we’ve made our web service calls, we can output the results. To get a full view of what is returned in those responses, I recommend using cfdump for a quick look.
  • rssfResponse1 only has a single result, so the output is relativity simple (you’re only running one current user when logging into the web service)
  • rssfResponse2 returns multiple records, so we need to get the total array length of the return, and loop over the result. As you can see in the sample above, getting the first and last name is not as simple as you might be used to, but the above sample works well (a lot of the older examples that are out there I was not able to get working). If the code looks vaguely familiar, it’s because I based it on Accessing Query Results from a Relationship Query from the Partner WSDL with Axis for Java article available

Using Salesforce.com web services with Coldfusion can be a somewhat frustrating experience (a lot of emails I’ve gotten in the past month prove that), so hopefully this article helps make just a little more sense of returning data with the Partner WSDL that Salesforce.com makes available.