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

470 Views Asked by At

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>
2

There are 2 best solutions below

0
Henk van Boeijen On BEST ANSWER

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.
0
Harinarayanan On

The best way to converge both fetchxml is below. Basically I did following things

  1. Removed all unique conditions and added attributes for every condition. Once you retrieve you can filter it later in the entity collection

  2. Combined all condition attributes in filter or condition together, this should again be filtered in entity collection via code

    <?xml version="1.0" encoding="UTF-8"?>
    <fetch>
        <entity name="msdyn_workorder">
            <attribute name="msdyn_timeclosed" />
            <attribute name="tel_wo_trigger_installedparts_integration" />
            <filter type="and">
                <condition attribute="statecode" operator="neq" value="1" />
                <condition attribute="wo_regionname" operator="in" />
            </filter>
            <link-entity name="msdyn_workorderproduct" from="msdyn_workorder" to="msdyn_workorderid" link-type="inner" alias="wop">
                <attribute name="tel_wo_installedqtysubmitstatus" />
                <filter type="and">
                    <condition attribute="statecode" operator="neq" value="1" />
                    <condition attribute="tel_wo_unknownpart" operator="neq" value="1" />
                </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" />
                        <condition attribute="tel_wo_code" operator="eq" value="001002" />
                        <condition attribute="tel_wo_code" operator="eq" value="002001" />
                        <condition attribute="tel_wo_code" operator="eq" value="002002" />
                        <condition attribute="tel_wo_code" operator="eq" value="002003" />
                        <condition attribute="tel_wo_code" operator="eq" value="004004" />
                    </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" />
                            <condition attribute="tel_wo_code" operator="eq" value="002" />
                            <condition attribute="tel_wo_code" operator="eq" value="004" />
                        </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>