web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Answered

Retrieve Only Latest Record On Linked Entity - FetchXML

(0) ShareShare
ReportReport
Posted on by 135

Hi, 

I am trying to edit some FetchXML so that, on a linked entity, it only retrieves the most recent entry. 

I am looking for a list of contacts who, on their most recent linked entity (A) record, have a linked entity (B) record with certain values. 

I can build most of the query in advanced find but what i cannot seem to do is limit entity A to only the most recent record. 

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
  <entity name="contact" >
    <attribute name="fullname" />
    <attribute name="telephone1" />
    <attribute name="emailaddress1" />
    <attribute name="new_practice" />
    <attribute name="new_patientscheme" />
    <attribute name="new_oasispatientid" />
    <attribute name="new_nhsnumber" />
    <attribute name="mobilephone" />
    <attribute name="jacrm_isanadult" />
    <attribute name="gendercode" />
    <attribute name="createdon" />
    <attribute name="address1_postalcode" />
    <order attribute="createdon" descending="true" />
    <order attribute="fullname" descending="false" />
    <filter type="and" >
      <condition attribute="statecode" operator="eq" value="0" />
    </filter>
    <link-entity name="cdi_postedsubscription" from="cdi_contactid" to="contactid" alias="aa" >
      <link-entity name="cdi_subscriptionpreference" from="cdi_postedsubscriptionid" to="cdi_postedsubscriptionid" alias="ab" >
        <filter type="and" >
          <condition attribute="cdi_subscriptionlistid" operator="eq" uiname="Latest Oasis Dental Care News" uitype="cdi_subscriptionlist" value="{6DD8EF28-A842-E611-80EB-005056811B80}" />
          <condition attribute="cdi_preference" operator="eq" value="1" />
        </filter>
      </link-entity>
    </link-entity>
    <attribute name="contactid" />
  </entity>
</fetch>


Any and all help apprecaited!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello Joel,

    That query definitely possible with t-SQL but not possible with FetchXml. You will have to find other way of accomplishing your goal.

  • Joel Abbott - xRM Consultant Profile Picture
    135 on at

    Thanks Andrii, would you be able to give me any pointers on doing it in t-SQL? Thanks J

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Do you work with On-Premise? SQL-Based reports are not applicable in CRM Online.

  • Joel Abbott - xRM Consultant Profile Picture
    135 on at

    Yes this is for CRM 2015 On-Premise

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Should be something similar to following:

    ;with vals as (

    Select

    c.contactid

    ,ab.cdi_subscriptionlistid

    ,ab.cdi_preference

    ,ROW_NUMBER() over (partition by c.contactid order by ab.createdon desc) rownumber

    From FilteredContact c

    Inner Join Filteredcdi_postedsubscription aa on aa.cdi_contactid = c.contactid

    Inner Join Filteredcdi_subscriptionpreference ab on aa.cdi_postedsubscriptionid = ab.cdi_postedsubscriptionid

    Where c.statecode = 0

    )

    ,vals2 as (Select * From vals Where rownumber = 1)

    Select

    c.fullname

    ,c.telephone1

    --add here all fields you need

    From FilteredContact c

    Inner Join vals2 on c.contactid = vals2.contactid

    where

    vals2.cdi_subscriptionlistid = '{6DD8EF28-A842-E611-80EB-005056811B80}'

    And vals2.cdi_preference = 1

  • Joel Abbott - xRM Consultant Profile Picture
    135 on at

    Many thanks!

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Welcome! Does it work?

  • Suggested answer
    Phuongtran Profile Picture
    90 on at

    Hi,

    I'm thinking about count in fetch xml. For example:

    <fetch count="1">


    I remembered that I tried before (use with sort by modified on desc)

    In this case, you sorted with created on desc, so I think you can try this

    Hope this helps,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Shidin Haridas Profile Picture

Shidin Haridas 2

#2
Abdullah13 Profile Picture

Abdullah13 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans