Wednesday 21 June 2017

{know-how} Or condition between linked entities or multiple entites in MSCRM Fetchxml

I have seen many struggling to achieve an OR condition between filters in parent entity and the linked entity. So today lets see how to overcome this issue and how to achieve an OR condition spanning between multiple entities.

Scenario:
I want to select an account with specific phone number or its primary contacts phone number is equal to the given number

Gap:
In this scenario we may not able to form an or condition for the filters as the filters are split between the parent(account) and the linked entity(contact) in advanced find editor. But the same can be achieved in fetchxml.

Solution:
Fetchxml supports conditions from other linked entities, the trick is to use an attribute called entityname in condition node to specify which entity it links to. The same is highlighted below.



<fetch version="1.0" output-format="xml-platform" mapping="logical" >
    <entity name="account" >
        <attribute name="accountid" />
        <filter type="or" >
            <condition attribute="telephone1" operator="eq" value="1234" />
            <condition entityname="contact" attribute="telephone1" operator="eq" value="1234" />
        </filter>
        <link-entity name="contact" alias="contact" from="contactid" to="primarycontactid" />
    </entity>
</fetch>

Hope this helps!

9 comments:

  1. Nice and useful information about Dynamics CRM . I really liked it.
    Good work.

    Dynamics AX Developer

    ReplyDelete
  2. Great one, did not know about this

    ReplyDelete
  3. Yes but what can you do with the edited fetchxml? Can you reupload it to a new Personal view?

    ReplyDelete
  4. Can we do that with a deep relationship? A second-degree relationship

    ReplyDelete
    Replies
    1. tried that, did not work. See below, I need to find records that [either comply with condition in related entity 1 (ema_product) or condition in related entity 2 (ema_scientificcontent)] AND comply with the condition in the main entity (incident). That seems impossible to do, at least for me...


























      {9EA5C3B9-5493-EA11-A811-000D3A38A16C}
      {CB11750E-7593-EA11-A811-000D3A38A16C}
      {6E494C82-7593-EA11-A811-000D3A38A16C}
      {D9D49B55-C762-EA11-A811-000D3A3A10EA}
      {74F51693-C762-EA11-A811-000D3A3A10EA}
      {E96722F9-1C8A-EA11-A811-000D3A3A10EA}



































      Delete