Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

FetchXML to retrieve last child record

Posted on by 15

I'm building a report for my CRM app and need to figure out how to write the FetchXML statement.  I have two custom entities in a parent-child relationship.  I need to select all fields from both parent and child entities but for the child entity I only want to retrieve the latest record.  I have seen examples of how to achieve this using "groupby" and "aggregate" clauses but they only bring back the date column.  How do I fetch all the columns in the child entity?

  
    
    
    
    
      
        
      
    
  

  • Verified answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: FetchXML to retrieve last child record

    Hi,

    It cannot be achieve by fetchXML only. "groupby" and "aggregate" can only show the column which has been set with "aggregate".

    1. The "order" can be used to order your results by the id of the parent entity and the date of the child entity. But it cannot show the latest one only.

    Here is an example to show the contacts of the accounts order by the account ids and the create date of contacts.

     

      
        
        
        
          
          
          
          
          
        
      
    

     

    1. As I know that what you need is not just fetchXML but to build a report. I did some search about it and find a similar issue.

    ( ) How to get Top Row Only In A Link-Entity in FetchXML Report? - Microsoft Dynamics CRM Forum Community Forum

    Here is the solution used by the poster.

    reporting services - Auto numbering to parent and child columns - Stack Overflow

     

    1. Or you can create a custom field in the parent entity and when a child record is updated, the field will be updated to a value that uniquely identifies this child record.
    2. It is also recommended to try using other ways of generating reports than fetchXML.

     

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Nya

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans