Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Phuongtran Profile Picture
    Phuongtran 90 on at
    RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    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,

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    Welcome! Does it work?

  • RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    Many thanks!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    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

  • RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    Yes this is for CRM 2015 On-Premise

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Retrieve Only Latest Record On Linked Entity - FetchXML

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

  • RE: Retrieve Only Latest Record On Linked Entity - FetchXML

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

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Retrieve Only Latest Record On Linked Entity - FetchXML

    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.

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

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans