Search code examples
dynamics-crmfetchxml

Combining 2 FetchXML With Same Dataset but different filters in Linked Entities


Is it possible to combine 2 FetchXMLs, which are fetching same data, but different filters in linked entities?

Below are 2 FetchXMLs that I am trying to merge to optimize performance. I marked each filter condition as //common or //unique to each query.

FetchXML (1)

    <fetch>
      <entity name="msdyn_workorder" >
        <filter type="and" >
          <condition attribute="statecode" operator="neq" value="1" /> //Common
          <condition attribute="wo_regionname" operator="in" >@[$Project::IncludeRegion]</condition> //Common
        </filter>
        <link-entity name="msdyn_workorderproduct" from="msdyn_workorder" to="msdyn_workorderid" link-type="inner" alias="wop" >
          <filter type="and" >
            <condition attribute="statecode" operator="neq" value="1" /> //Common
            <condition attribute="tel_wo_unknownpart" operator="neq" value="1" /> //Common
            <condition attribute="tel_wo_installedqtysubmitstatus" operator="eq" value="100000200" /> //Unique
          </filter>
          <link-entity name="uom" from="uomid" to="msdyn_unit" link-type="outer" alias="uom" >
            <attribute name="name" />
          </link-entity>
          <link-entity name="tel_wo_partsubcategory" from="tel_wo_partsubcategoryid" to="tel_wo_subcategory" link-type="inner" alias="subcategory" >
            <filter type="or" >
              <condition attribute="tel_wo_code" operator="eq" value="001001" /> //Common
              <condition attribute="tel_wo_code" operator="eq" value="002002" /> //Common
            </filter>
            <link-entity name="tel_wo_partcategory" from="tel_wo_partcategoryid" to="tel_wo_partcategory" link-type="inner" alias="category" >
              <filter type="or" >
                <condition attribute="tel_wo_code" operator="eq" value="001" /> //Common
                <condition attribute="tel_wo_code" operator="eq" value="002" /> //Common
              </filter>
            </link-entity>
          </link-entity>
          <order attribute="msdyn_workorderproductid" />
        </link-entity>
        <link-entity name="tel_com_salesorderheader" from="tel_com_salesorderheaderid" to="tel_wo_primary_so" link-type="outer" alias="primaryso" >
          <attribute name="tel_com_sales_document_no" />
        </link-entity>
        <link-entity name="territory" from="territoryid" to="wo_region" link-type="inner" alias="Region" >
          <filter>
            <condition attribute="tel_com_sendcs0130afterinstalledqtychanged" operator="eq" value="1" /> //Unique
          </filter>
        </link-entity>
      </entity>
    </fetch>

FetchXML (2)

    <fetch>
      <entity name="msdyn_workorder" >
        <filter type="and" >
          <condition attribute="msdyn_timeclosed" operator="not-null" /> //Unique
          <condition attribute="tel_wo_trigger_installedparts_integration" operator="eq" value="1" /> //Unique
          <condition attribute="statecode" operator="neq" value="1" /> //Common
          <condition attribute="wo_regionname" operator="in" >@[$Project::IncludeRegion]</condition> //Common
        </filter>
        <link-entity name="msdyn_workorderproduct" from="msdyn_workorder" to="msdyn_workorderid" link-type="inner" alias="wop" >
          <filter type="and" >
            <condition attribute="statecode" operator="neq" value="1" /> //Common
            <condition attribute="tel_wo_unknownpart" operator="neq" value="1" /> //Common
          </filter>
          <link-entity name="uom" from="uomid" to="msdyn_unit" link-type="outer" alias="uom" >
            <attribute name="name" />
          </link-entity>
          <link-entity name="tel_wo_partsubcategory" from="tel_wo_partsubcategoryid" to="tel_wo_subcategory" link-type="inner" alias="subcategory" >
            <attribute name="tel_wo_code" />
            <filter type="or" >
              <condition attribute="tel_wo_code" operator="eq" value="001001" /> //Common
              <condition attribute="tel_wo_code" operator="eq" value="001002" /> //Common
              <condition attribute="tel_wo_code" operator="eq" value="002001" /> //Unique
              <condition attribute="tel_wo_code" operator="eq" value="002002" /> //Unique
              <condition attribute="tel_wo_code" operator="eq" value="002003" /> //Unique
              <condition attribute="tel_wo_code" operator="eq" value="004004" /> //Unique
            </filter>
            <link-entity name="tel_wo_partcategory" from="tel_wo_partcategoryid" to="tel_wo_partcategory" link-type="inner" alias="category" >
              <attribute name="tel_wo_code" />
              <filter type="or" >
                <condition attribute="tel_wo_code" operator="eq" value="001" /> //Common
                <condition attribute="tel_wo_code" operator="eq" value="002" /> //Common
                <condition attribute="tel_wo_code" operator="eq" value="004" /> //Unique
              </filter>
            </link-entity>
          </link-entity>
          <order attribute="msdyn_workorderproductid" />
        </link-entity>
        <link-entity name="tel_com_salesorderheader" from="tel_com_salesorderheaderid" to="tel_wo_primary_so" link-type="outer" alias="primaryso" >
          <attribute name="tel_com_sales_document_no" />
        </link-entity>
        <link-entity name="territory" from="territoryid" to="wo_region" link-type="inner" alias="Region" >
          <attribute name="tel_com_sendcs0130afterinstalledqtychanged" />
        </link-entity>
      </entity>
    </fetch>

Solution

  • Merging these FetchXML queries would require an SQL UNION like construction and unfortunately this is not supported by FetchXML.

    There are two options:

    1. Keep these queries separate.
    2. Create one query selecting slightly more rows than actually needed and filter the results afterwards.