function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ldtechldtech 

Retrieving simple query results

I've got a relatively simple little setup to test out retrieving some salesforce data.  I can't quite get the records to retrieve the data inside them however (like first name, last name, etc.).  Here's what I've got:

<cfset queryName = sfObject.query("SELECT firstname FROM contact")>
<cfset queryRecords = queryName.getRecords()>
<cfdump var="#ArrayLen(queryRecords)#">
<cfdump var="#sfObject.describeGlobal().getTypes()#">

As you can see I'm using CF.  I was breaking down the objects with cfdump and figuring out what methods were inside each class, but I've come to a stopping point where I can't figure out how to retrieve the exact data I need (such as first name).  The query executes correctly (as I tested incorrect ones out to make sure) but using things like:

queryRecords.firstname
or
queryRecords[1].firstname

doesn't work (although the second one appears to be more correct).  There should be 21 objects inside of queryRecord so queryRecord[1] should be perfectly fine (per the array length check).  Other websites I've checked out have done it just like this, with a loop around what should be the record result set to output all of the results they retrieve.

Thanks in advance :)
Michael.WatkinsMichael.Watkins
i believe the queryresults actually return sfobjects which you can then access the properties, as in the following example:

Dim oResult As New sforce.QueryResult
oResult = oProxy.query("SELECT ID, ContactId, CaseNumber, Subject, Status FROM Case")

If Not oResult.records Is Nothing AndAlso oResult.records.Length > 0 Then
    Dim oCase As sforce.Case
    Dim oTable As New System.Data.DataTable

    oTable.Columns.Add("ID")
    oTable.Columns.Add("ContactID")
    oTable.Columns.Add("CaseNumber")
    oTable.Columns.Add("Subject")
    oTable.Columns.Add("Status")
    oDataSet.Tables.Add(oTable)

    For Each oCase In oResult.records
        Dim oDataRow As DataRow = oDataSet.Tables(0).NewRow
        oDataRow.Item(0) = oCase.Id
        oDataRow.Item(1) = oCase.ContactId
        oDataRow.Item(2) = oCase.CaseNumber
        oDataRow.Item(3) = oCase.Subject
        oDataRow.Item(4) = oCase.Status
        oDataSet.Tables(0).Rows.Add(oDataRow)
    Next
End If


ldtechldtech
Yes, I get the SObject (which I assume is the same thing you're referring to) but I don't see any methods or fields from which to draw out the data I'm looking for.  Also, as a side note, there should only be one record in my 'Contacts' table, since I've only created one contact as far as I know.  Here is the dump from my "queryRecords" variable (note, there are no fields displayed):

object of com.sforce.soap.partner.sobject.SObject

Class Name     com.sforce.soap.partner.sobject.SObject

Methods    

Method     Return Type
equals(java.lang.Object)     boolean
getDeserializer(java.lang.String, java.lang.Class, javax.xml.namespace.QName)     org.apache.axis.encoding.Deserializer
getFieldsToNull()     java.lang.String[]
getFieldsToNull(int)     java.lang.String
getId()     java.lang.String
getSerializer(java.lang.String, java.lang.Class, javax.xml.namespace.QName)     org.apache.axis.encoding.Serializer
getType()     java.lang.String
getTypeDesc()     org.apache.axis.description.TypeDesc
get_any()     org.apache.axis.message.MessageElement[]
hashCode()     int
setFieldsToNull(int, java.lang.String)     void
setFieldsToNull(java.lang.String[])     void
setId(java.lang.String)     void
setType(java.lang.String)     void
set_any(org.apache.axis.message.MessageElement[])     void

P.S. That appears to be VB, I'm hopefully looking for CF solution but I can understand both if it applies to CF :)


Message Edited by ldtech on 04-17-2008 11:34 AM
Michael.WatkinsMichael.Watkins
i believe sObject is generic- you need to cast it to the object type of contact to access the fields.

not sure the syntax in CF, but loop thru the resultset and cast the result as a contact:

something like:
for each oContact as sforce.contact in [your_query_result_set]

once it is cast, you can access the fields of each record like this:
[your_variable_name] = oContact.FirstName

etc

and joined resultsets might look like:
oContact.LastModifiedBy.Name  or
oContact.CreatedBy.DateCreated 

etc


ldtechldtech
Anyone schooled in ColdFusion know how to cast this generic SOBject into a 'contact' or other query object so I can retrieve the results from it? :)  I'm not up on my complex object casting in CF :p


Message Edited by ldtech on 04-17-2008 12:40 PM
ldtechldtech
After exploring a bit further I found the Enterprise WSDL has all the neccesary fields available to me (such as Account, Campaign, etc.).  I realize I'd have to update it whenever something changed on the Salesforce side, but as a quick plug, couldn't I use the Enterprise version to create my objects?
ldtechldtech
The answer to my own question is yes.  I can access it through the Enterprise WSDL and have access to all the methods and objects I need thankfully :)

What are the possibilities of the SalesForce dev team writing up a small custom Java class to incorporate it with ColdFusion?  It wouldn't be a huge leap to just take some of the Java code you guys already use and put it over there would it?  I'm sure it would solve other people's problem incorporating CF and SalesForce together without the need to write their own Java classes just to get it integrated.

Also as some constructive criticism, I found the 'wiki' hard to find my way around in general.  The pages were laid out in huge lists with very little differentiation between areas, and the tutorials were very inconsistant in quality.  One of the lower rated ones would be the presentation where someone builds a small sample app in a video taped demonstration but it's done ENTIRELY OFFSCREEN.  There's a LOT of material that repeats itself and the API guides written up in very nice AJAX programs are needlessly split up.  There's only a handful of information in each for some reason.  Lastly, maybe I'm just spoiled for Java docs, but I think it would be helpful if the actual method calls for the base Salesforce setup were available somewhere and the object heirarchy so someone working in a "lesser" language like myself could easily tell what calls what instead of having to dump each individual object to find out what calls I need to make in order to access the info I want.

Overall, looks like a promising bit of development you have going on ;)


Message Edited by ldtech on 04-18-2008 09:30 AM
mshelmanmshelman
yes the partner wsdl is more difficult to use although it's use does have advantages. cf webservices support is of course built on apache axis so you would want to look at the sales force partner samples for java to see how to access partner wsdl in cf.

so anyway referring to your dump of sobject above getAny returns an array of fields (axis MessageElement objects) which in turn have getName and getValue methods. getId returns the id of the sobject and getType would tell you the type of sobject that you're dealing with