Oh my god. It's full of code!

Posts tagged “Integration

Coldfusion describe sObject function

Hey all!

So this time I have another handy function for interacting with Salesforce from Coldfusion. If you don’t know, I’m a huge fan of working with the Salesforce API with Coldfusion using the library by Tom De Manicor http://www.escapekeys.com/blog/index.cfm/.

I’ve been using it so much, and so heavily that here and there I make some small add ons and other neat stuff. This function in particular is one I really like. It allows you to describe an sObject and store all data about the fields in an easy to reference object. So say on your ColdFusion powered website you want to make a picklist with the same values as a picklist in Salesforce, and not have to hard code them, you can describe the sObject, find the fieldname key, and iterate over the values to build your picklist. Of course describe sObject is kind of an expensive call, so I’d recommend caching the results in application or server scope. I generally do this when my application starts and have a manual refresh when needed (how often does the schema of your object change, honestly?). Here is the function.

<cffunction name="CacheSFObject" access="public" hint="GetInfo About an Object from XML and store in an easy to use object">
    <cfargument name="ObjectName" type="string" required="yes">
    <cfset var Object = server.OSF.describeObject(arguments.ObjectName)>
    <cfset var ObjectInfo = Object.RawSoap>
    <cfset var returnStruct = structnew()>
            <cfset ObjectFieldReference = ObjectInfo.Envelope.Body.describeSobjectResponse.result.fields[i]>
            <cfset returnStruct[ObjectFieldReference.name.xmltext] = structnew()>
            <cfset returnStruct[ObjectFieldReference.name.xmltext].name=ObjectFieldReference.name.xmltext>
            <cfset returnStruct[ObjectFieldReference.name.xmltext].type=ObjectFieldReference.type.xmltext>
            <cfset returnStruct[ObjectFieldReference.name.xmltext].label=ObjectFieldReference.label.xmltext>
            <cfset returnStruct[ObjectFieldReference.name.xmltext].updateable=ObjectFieldReference.updateable.xmltext>
            <cfset returnStruct[ObjectFieldReference.name.xmltext].calculated=ObjectFieldReference.calculated.xmltext>
            <cfif trim(ObjectFieldReference.type.xmltext) EQ "reference">
                <cfset returnStruct[ObjectFieldReference.name.xmltext].refto=ObjectFieldReference.referenceto.xmltext>
            <cfif trim(ObjectFieldReference.type.xmltext) EQ "picklist" or trim(ObjectFieldReference.type.xmltext) EQ "multipicklist">
                <cfset returnStruct[ObjectFieldReference.name.xmltext].values = arraynew(1)>
                <cfloop from="1" To="#arraylen(ObjectFieldReference.picklistValues)#" index="j">
                    <cfset returnStruct[ObjectFieldReference.name.xmltext].values[j] = ObjectFieldReference.picklistValues[j].label.xmlText>
        <cfcatch type="any">
    <cfreturn returnStruct>

The function does reference the Salesforce component by Tom De Manicor, so you’ll need to download that and have it set up before my function works. You’ll have to change the object reference near the top as well unless you happen to reference it exactly the same way I do. So then to use it, just call

<cfset server.SalesforceFields.Contact = CacheSFObject('Contact')>

Now if you dump out of the contents of server.SalesforceFields.contact you’ll get something that looks like.

Sample of contact describe dump

Sample of contact describe dump

So there ya go, another handy function. Hope someone digs this as much as I do 😉