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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

FetchXML to retrieve last child record

(0) ShareShare
ReportReport
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?

  
    
    
    
    
      
        
      
    
  

I have the same question (0)
  • Verified answer
    Nya Profile Picture
    29,060 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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 172

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 116 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans