Simon Fell > Its just code > What the hell, why are there 2 Ids ?

Tuesday, July 25, 2006

This question comes up every now and then, someone takes a look at the response from the API query call and notices that Id is in there twice, its broke, come on, fix it already, damn on-demand slackers! e.g.

<soapenv:Envelope xmlns:soapenv="" 
xmlns:xsi="" xmlns="" xmlns:sf="">
  <result xsi:type="QueryResult">
   <queryLocator xsi:nil="true"/>
   <records xsi:type="sf:sObject">

Actually, what's going is is that the Id is suposed to be there twice, it makes more sense once you've looked at the WSDL, which defines sObject as

<complexType name="sObject">
    <element name="type" type="xsd:string"/>
    <element name="fieldsToNull" type="xsd:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/> 
    <element name="Id" type="tns:ID" nillable="true"/>
    <any namespace="##targetNamespace" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>

The first Id element is there to populate the explicit Id element in the sObject definition, the 2nd Id is there because you included it in the field list for your query (in this case the query was select id, firstname, lastname, accountId from contact where ....) We do this so that the any collection (the collection of elements that are mapped into the any element in the schema, and are typically exposed as an array of elements in the toolkits) contains exactly one element for every field you asked for in the query, and they're in the order you specified in your query, this makes it easier on the client to index into the array and grab the exact data they're expecting, without having to map the elements back to a dictionary to look them up by name. On the downside, if you're using a tool that's not driven by the WSDL, then this can cause all sorts of confusion. Its interesting (and annoying) to note how the tools and machinations of schema mean you end up distorting the messages you generate, If you were ignoring WSDL, Schema and the WS tools, the response would actually be something more like this.

<soapenv:Envelope xmlns:soapenv="" xmlns="">

I think the thing that's really going to bug me down the road is the fact that each generation of tooling will want a different set of tweaks, and what works today for Axis 1.x and .NET 1.x isn't going to work for Axis 3.0 and Indigo II, will the tools ever save me, or just send me to the funny farm to drink coffee all day?