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:

"1.0" output-format="xml-platform" mapping="logical" distinct="true" >
  "incident" >
    "incidentid" />
    "title" />
    "customerid" />
    "xrm_businessname" />
    "statecode" />
    "statuscode" />
    "or" >
      "ad" attribute="xrm_contactid" operator="eq" value="" />
      "ab" attribute="xrm_contactid" operator="eq" value="" />
    "account" from="accountid" to="customerid" link-type="outer" alias="ac" >
      "xrm_accountcontact" from="xrm_accountid" to="accountid" link-type="outer" alias="ad" >
    "xrm_casecontact" from="xrm_caseid" to="incidentid" link-type="outer" alias="ab" />

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.