Search code examples
dynamics-crmmicrosoft-dynamicsdynamics-365fetchxmldynamics-crm-365

Left Join fetchXml based on common column


I have two tables that are not related, but have a common column. I want to join the rows in table B that match with <attribute name='substitutedproductid'> in Table A. Table A looks like this:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="productsubstitute">
    <attribute name='substitutedproductid' />
    <attribute name='new_quantity' />
    <attribute name='new_unit' />
    <attribute name='new_grouping' />
    <filter type="and">
      <condition attribute="productid" operator="eq" value="{guid}" />
    </filter>
  </entity>
</fetch>

Table B looks like this:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="new_contractlinedislike">
    <attribute name="new_contractlinedislikeid" />
    <attribute name="substituteproductid" />
    <attribute name="new_unit" />
    <attribute name="new_quantity />
    <attribute name="new_grouping" />
    <filter type="and">
      <condition attribute="new_contractlineid" operator="eq"  value="{guid}" />
    </filter>
  </entity>
</fetch>

As shown above, both tables have a lookup (substituteproductid), which can be to the same product. In the case that the lookups are the same, I want to do a left join onto Table A. Basically, I want to return a single entitycollection from a single fetchXml string. How can I achieve this?


Solution

  • Basically you have to use link-entity. This query should work for your scenario. Referred the old discussion.

    <fetch version='1.0' output-format='xml-platform' mapping='logical' >
      <entity name= 'productsubstitute' >
        <attribute name='new_quantity' />
        <attribute name='new_unit' />
        <attribute name='new_grouping' />
        <filter type='and' >
          <condition attribute='productid' operator='eq' value= '{guid}' />
        </filter>
        <link-entity name='new_contractlinedislike' from='substitutedproductid' to='substituteproductid' link-type='outer' >
          <attribute name='new_contractlinedislikeid' />
          <attribute name= 'new_grouping' />
        <filter type='and' >
          <condition attribute='new_contractlineid' operator='eq' value= '{guid}' />
        </filter>
        </link-entity>
      </entity>
    </fetch>