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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve the last record in an entity

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Mihir Maruti Kadam Profile Picture
    702 on at

    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

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at
    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();
  • Community Member Profile Picture
    on at

    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

  • ashlega Profile Picture
    34,477 on at

    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)

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans