FetchXML: get count of records based on a field of a linked entity

Neodine picture Neodine · Jun 9, 2014 · Viewed 7.5k times · Source

I have a scenario where i want to get the count of people from an Entity who have multiple location address marked as current address in other linked entity.

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true">
<entity name="client">
<attribute name="clientid"/>
<attribute name="name"/>
<attribute name="createdon"/>
<order attribute="name" descending="false"/>
<link-entity name="locationadd" from="clientid" to="clientid" alias="aa">
  <attribute name="locationadd" aggregate="countcolumn" />
  <filter type="and">
    <condition attribute="isthiscurrentadd" operator="eq" value="1"/>
  </filter>
  </link-entity>
  </entity>
</fetch>

Answer

Neodine picture Neodine · Jun 24, 2014

The answer is

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true'> 
<entity name='client'> 
<attribute name='clientid' alias='PersonName' groupby='true' /> 
<link-entity name='locationadd' from='clientid' to='clientid' alias='aa'> 
<attribute name='isthiscurrentadd' alias='Count' aggregate='count'/> 
<filter type='and'> 
<condition attribute='isthiscurrentadd' operator='eq' value='1'/> 
</filter> 
</link-entity> 
</entity> 
</fetch>