Multiple Fetch Xml Statements with Or Filter in Portal Web Template

Multiple Fetch Xml Statements with Or Filter in Portal Web Template

In a recent project, we had a requirement to display cases to the portal that are associated with either Contacts or Accounts. Our case entity had a related entity called Case Contacts, and our account entity had a related entity called Account Contacts. The account entity had a list of all the cases associated with the account.

The issue that we had was that we could retrieve each list of cases individually, but the result could be duplicate where a case could be associated with both a Case Contact and an Account Contact.

We were struggling on whether to retrieve the first fetch, and then the second fetch and check the values that were part of the second fetch didn’t appear in the list of first fetches, or the alternative was to have a single fetch Xml that contained both conditions with an Or Condition in-between. The issue here is that advanced find does not allow created an Or condition with related entities.

Thanks to a post by Alex Shlega, we were able to do this easily. This can be done using XRMToolbox Fetch Xml Builder or just writing the Fetch Directly in a Text editing tool. The end result that we had was the following:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
  <entity name="incident" >
    <attribute name="incidentid" />
    <attribute name="title" />
    <attribute name="customerid" />
    <attribute name="xrm_businessname" />
    <attribute name="statecode" />
    <attribute name="statuscode" />
    <filter type="or" >
      <condition entityname="ad" attribute="xrm_contactid" operator="eq" value="{{user.id}}" />
      <condition entityname="ab" attribute="xrm_contactid" operator="eq" value="{{user.id}}" />
    </filter>
    <link-entity name="account" from="accountid" to="customerid" link-type="outer" alias="ac" >
      <link-entity name="xrm_accountcontact" from="xrm_accountid" to="accountid" link-type="outer" alias="ad" >
      </link-entity>
    </link-entity>
    <link-entity name="xrm_casecontact" from="xrm_caseid" to="incidentid" link-type="outer" alias="ab" />
  </entity>
</fetch>

By adding the entity names to the Or Filter, we are able to specify conditions for the linked entities, and by doing this filter multiple linked entities with an Or Condition.

This resolves the issue of having to deal with duplicates in the results or having multiple result sets from separate Xml statements.