Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrieve the last record in an entity

Posted on by Microsoft Employee

Hi experts , 
I am writing a C# plugin and I want to access CRM database to retrieve the last record in an customized entity , How could I do that ?

In other words I want to get the same result of this query but using C#
SELECT TOP 1 * FROM MyCustomEntityName ORDER By CreatedOn DESC

*This post is locked for comments

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Retrieve the last record in an entity

    Hi Martina,

     what kind of plugins is that? Is it registered on the post-create of the same entity? If yes, you may be getting the id of the entity being created every time (might want to move it to the pre-create, for example)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve the last record in an entity

    Hi Ivan ,

    thanks for your quick response which I really appreciate  

    when I run your attached commend and then I checked the ID for the resulted entity it gives me wrong ID, and when I tried that several times it was giving me different id values and non of these IDs exist in my database

  • Suggested answer
    Ivan Ficko Profile Picture
    Ivan Ficko 1,380 on at
    RE: Retrieve the last record in an entity
    var qe = new QueryExpression("MyCustomEntityName");
    qe.ColumnSet = new ColumnSet(true);
    qe.AddOrder("createdon",OrderType.Descending);
    qe.TopCount = 1;
    var entity = service.RetrieveMultiple(qe).Entities.FirstOrDefault();
  • Suggested answer
    Mihir Maruti Kadam Profile Picture
    Mihir Maruti Kadam 700 on at
    RE: Retrieve the last record in an entity

    This is simple fetchXML generated using Advanced Find. Just add the count to 1.

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

     <entity name="account">

       <attribute name="name" />

       <attribute name="primarycontactid" />

       <attribute name="telephone1" />

       <attribute name="accountid" />

       <attribute name="createdon" />

       <order attribute="createdon" descending="true" />

     </entity>

    </fetch>

    You can refer this link to execute fetchXML in c# plugin

    msdn.microsoft.com/.../gg328117.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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans