dynamics-crmmicrosoft-dynamicspower-automatefetchxmldataverse

Dynamics 365 FetchXML nested link-entity, not-in


It's a long shot, but in FetchXML I have an entity with a many-to-many relationship with another table. In short, the Contact entity is linked with the ConnectionRole entity through an intermediary table Connection.

What I want to find are Contacts who do NOT have a ConnectionRole of a certain criteria (e.g. Tester/Developer as per below). They can have other ConnectionRole types, but they can't have those ones.

<entity name="contact">
    <link-entity name="connection" from="record1id" to="contactid">
        <filter>
            <condition attribute="statecode" operator="eq" value="0">
        </filter>
        <link-entity name="connectionrole" from="connectionroleid" to="recordrole1id">
            <filter type="or">
                <condition attribute="name" operator="eq" value="Tester">
                <condition attribute="name" operator="eq" value="Developer">
            </filter>
        </link-entity>
    </link-entity>
</entity>

I've been messing around with the left outer joins in FetchXML and thought I might be able to do something like this: (in my head that reads logically as Contacts who do not have a (Connection which has a matching ConnectionRole) attached, but it just brings back nothing...)

<entity name="contact">
    <filter>
        <condition entityname="connection" attribute="record1id" operator="null" />
    </filter>
    <link-entity name="connection" from="record1id" to="contactid" link-type="outer">
        <filter>
            <condition attribute="statecode" operator="eq" value="0">
        </filter>
        <link-entity name="connectionrole" from="connectionroleid" to="recordrole1id">
            <filter type="or">
                <condition attribute="name" operator="eq" value="Tester">
                <condition attribute="name" operator="eq" value="Developer">
            </filter>
        </link-entity>
    </link-entity>
</entity>

Any ideas? It's eventually gearing towards going in a Power Automate Flow so I'm trying to avoid the alternative scenario of retrieving EVERY Contact and then filtering that way. It would be so much easier in SQL.

I've tried everything related to left outer joins in FetchXML however as soon as I go a layer deeper than a single link-entity it doesn't return any results let alone what I'm expecting.


Solution

  • The issue is with the inner join following the left outer join. Also you need to filter on connectionrole, not connection. Try this query instead:

    <fetch distinct='true' top='100'>
      <entity name='contact'>
        <attribute name='contactid' />
        <attribute name='fullname' />
        <link-entity name='connection' to='contactid' from='record1id' alias='cn' link-type='outer'>
          <link-entity name='connectionrole' to='record1roleid' from='connectionroleid' alias='cr' link-type='outer'>
            <filter>
              <condition attribute='name' operator='in'>
                <value>Developer</value>
                <value>Tester</value>
              </condition>
            </filter>
          </link-entity>
        </link-entity>
        <filter>
          <condition attribute='connectionroleid' entityname='cr' operator='null' />
        </filter>
      </entity>
    </fetch>