Tutorial :Can you write a single FetchXML query to get 1:many relationship?


Is it possible to write a single FetchXML query that gets a root entity and multiple children? All I've been able to do is 1:1.


No, it is not possible.


James Wood is correct. Fetch XML is recursive so by using the link entity you can get the information you want.

For example, the following is valid:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">    <entity name="account">      <attribute name="name" />      <attribute name="primarycontactid" />      <attribute name="telephone1" />      <attribute name="accountid" />      <order attribute="name" descending="false" />      <link-entity name="contact" from="parentcustomerid" to="accountid" alias="aj">          <attribute name="firstname" />          <attribute name="lastname" />          <attribute name="telephone1" />          <link-entity name="businessunit" from="businessunitid" to="owningbusinessunit" alias="ak">              <attribute name="name" />              <attribute name="address1_line1" />              <attribute name="address1_line2" />              <attribute name="address1_line3" />              <filter type="and">                <condition attribute="name" operator="not-null" />              </filter>          </link-entity>      </link-entity>    </entity>  </fetch>  

If your question really is "Is it possible to write a single FetchXML query that gets a SINGLE root entity and multiple children" then the answer is unfortunately no. However if you are able to handle duplicates of root data (For example using the Grouping functionality of an SSRS report) then what you require is entirely possible


Unless I've misunderstood the question this is very possible.

So for example you want to find all the contacts related to a given account. This is represented in Crm by the Parent Customer Lookup on the contact to the account.

enter image description here

<fetch mapping="logical" count="100" version="1.0">      <entity name="account">          <attribute name="name" />          <link-entity name="contact" from="parentcustomerid" to="accountid">              <attribute name="fullname" />          </link-entity>      </entity>  </fetch>  

Which gives you a result set that looks like this:

<resultset morerecords="0" paging-cookie="&lt;cookie page=&quot;1&quot;&gt;&lt;accountid last=&quot;{E704FAD6-2D4B-E111-9FED-00155D828444}&quot; first=&quot;{AD912122-6B3C-E111-9B37-00155D828444}&quot; /&gt;&lt;/cookie&gt;">      <result>          <name>RGD Mining Inc</name>          <accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>          <accountid.fullname>Bill Miner</accountid.fullname>      </result>      <result>          <name>RGD Mining Inc</name>          <accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>          <accountid.fullname>Green</accountid.fullname>      </result>  </resultset>  


I'm happy to report that it is possible. I have a solution that worked well for me.

The only caveat is that if you have multiple child accounts you will get multiple results for the parent. For example:

parent 1: child 1  parent 2: child 1  parent 2: child 2  

This means that you would then have to run the results through a sorting function, to get either all the children under parents in a multi dimensional array, or get all the accounts as unique entries in a flat array.

Also, this only goes down one level. If your hierarchy is multi-level, this code would need to be modified.

<fetch distinct="false" mapping="logical">      <entity name="account">               <attribute name="name" />          <link-entity name="account"  alias="childaccount" to="accountid" from="parentaccountid"  link-type="outer">              <attribute name="name" alias="childname" />          </link-entity>      </entity>             </fetch>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »