Search code examples
dynamics-crmmicrosoft-dynamicsfetchxml

Unable to pull contacts with NO accounts tied using CRM SDK FetchXML


Using the below fetchxml, it is pulling contacts only that has account tied up. I want to pull contacts that has an account tied up [if so account name] and also contacts with no account tied up together. I have outer join but still it doesn't work. The below fetchxml is pulling only contacts that has account number tied up but not all active contacts with no account information.

<fetch top="50" >
  <entity name="contact" >   
    <attribute name="emailaddress1" />
    <attribute name="lastname" />    
    <attribute name="firstname" />    
    <attribute name="accountidname" />
    <attribute name="accountid" />
    <filter type="and" >
      <condition attribute="statecode" operator="eq" value="1" />
    </filter>
    <link-entity name="account" from="accountid" to="accountid" link-type="outer" >
      <attribute name="name" />
    </link-entity>
  </entity>
</fetch>

Solution

  • I see only one issue, the attribute parentcustomerid in contact entity should be used instead of accountid.

    So the join condition should be:

    <link-entity name="account" from="accountid" to="parentcustomerid" link-type="outer" >

    Complete query:

    <fetch top="50" >
      <entity name="contact" >   
        <attribute name="emailaddress1" />
        <attribute name="lastname" />    
        <attribute name="firstname" />    
        <attribute name="accountidname" />
        <attribute name="accountid" />
        <filter type="and" >
          <condition attribute="statecode" operator="eq" value="1" />
        </filter>
        <link-entity name="account" from="accountid" to="parentcustomerid" link-type="outer" >
          <attribute name="name" />
        </link-entity>
      </entity>
    </fetch>
    

    Looks like this accountid in contact is just guid not a lookup and moreover a virtual field. Read more