Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get Top Row Only In A Link-Entity in FetchXML Report?

Posted on by 1,505

How can I create a fetchXML query which will retrieve only the top row from a linked-entity?
I have a situation in which I want a report which shows Account and the last Opportunity Closed regarding account, ie in fetchXML i'm trying something like:

<fetch distinct="false" no-lock="false" mapping="logical"><entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"><attribute name="name" alias="name" /><attribute name="customertypecode" alias="customertypecode" /><attribute name="address1_city" alias="address1_city" /><attribute name="industrycode" alias="industrycode" /><attribute name="accountid" /><link-entity name="opportunity" to="accountid" from="parentaccountid" link-type="outer" alias="opportunity1" enableprefiltering="1" prefilterparametername="CRM_FilteredOpportunity"><attribute name="new_partnername" alias="opportunity1_new_partnername" /><attribute name="name" alias="opportunity1_name" /><attribute name="new_microsofttechnologies" alias="opportunity1_new_microsofttechnologies" /><attribute name="createdon" /><attribute name="actualclosedate" alias="opportunity1_actualclosedate" /><order attribute='actualclosedate' descending='true' /><attribute name="opportunityid" /></link-entity></entity></fetch>

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    Hi ,

     i need to display only latest created on records from a linked entity and so i used the indexing thing  for the same but i am unable to filter and show those records which are the latest created on linked entity records .Can you please help and let me know the expression you used to filter the same.

    any help appreciated.

  • Verified answer
    ClixSol Profile Picture
    ClixSol 1,505 on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    Chintan Dave your response appreciated:

    i make it possible by following this article stackoverflow.com/.../auto-numbering-to-parent-and-child-columns

    by indexing on opportunity i display only 1 index opportunity row using Expression.

    it's working as required.

    Thanks.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    Its not possible to apply top (count) record on Linked entity.

    But You can invert your query by putting linked entity as main one . You will then apply your filter to the link entity. I suggest you use Advanced fine to create the query and then download the fetchxml and add the count="5".

    I hope you get my point.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    ClixSol, Sorry I misunderstood before.

    I think <link-entity> doesnt support count for getting top record but there should be some different way to achieve this.

    You can add filter to have this.

    Let me try this using advance find. meanwhile have look on this article.

    sliong.wordpress.com/.../crm-2011-fetch-xml-an-layout-xml-for-link-entity-attribute

  • ClixSol Profile Picture
    ClixSol 1,505 on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    Chintan Dave your response appreciated:

    But I need Top X from link-entity name="opportunity" . Find below sample code.

    <fetch distinct="false" no-lock="false" mapping="logical">

     <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount">

     <attribute name="name" alias="name" />

     <attribute name="customertypecode" alias="customertypecode" />

     <attribute name="address1_city" alias="address1_city" />

     <attribute name="industrycode" alias="industrycode" />

     <attribute name="accountid" />

    <link-entity name="opportunity" to="accountid" from="parentaccountid" link-type="outer" alias="opportunity1" enableprefiltering="1" prefilterparametername="CRM_FilteredOpportunity">

     <attribute name="new_partnername" alias="opportunity1_new_partnername" />

     <attribute name="name" alias="opportunity1_name" />

     <attribute name="new_microsofttechnologies" alias="opportunity1_new_microsofttechnologies" />

     <attribute name="createdon" />

     <attribute name="actualclosedate" alias="opportunity1_actualclosedate" />

     <order attribute='actualclosedate' descending='true' />

     <attribute name="opportunityid" />

     </link-entity>

    </entity>

    </fetch>

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    You need to pass Top X in FetchQuery in count. Find below sample code to fetch top 10 records.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" count="10">

     <entity name="account">

       <attribute name="name" />

       <attribute name="primarycontactid" />

       <attribute name="telephone1" />

       <attribute name="accountid" />

       <order attribute="name" descending="false" />

     </entity>

    </fetch>

  • ClixSol Profile Picture
    ClixSol 1,505 on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    How can I create a fetchXML query which will retrieve only the top 1 row from a linked-entity.?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get Top Row Only In A Link-Entity in FetchXML Report?

    If you want to use FetchXML in your plugin/Custom Workflow you will need to download the FetchXML from your advanced find.  There is a very helpful button on the advanced find that appears after you have run the advanced find, which you can see below.

    So build the query here with the record you want in your report, and then download FetchXML and use it. 

    here is the link which will help you:

    https://community.dynamics.com/crm/b/crmbusiness/archive/2013/12/23/build-queries-with-fetchxml-instead-of-queryexpression.aspx

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans