Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

How to get only last record in table by using query in AX 2012

(0) ShareShare
ReportReport
Posted on by 630

Hello Friends,

I want to get only last record from table by using query in Ax 2012.

I am using below code.

queryBuildDataSource.addSortField(fieldNum(ABC, RecId), SortOrder::Descending);

By using above code I am getting all records but I want to get only last record.

Please tell me how to get only last record from query.

Thanks & Regards

Rahul

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: How to get only last record in table by using query in AX 2012

    Yes, RecId is not the right field for sorting. There is no guarantee.

  • Martin Dráb Profile Picture
    233,326 Most Valuable Professional on at
    RE: How to get only last record in table by using query in AX 2012

    If the goal is getting the record that was inserted last, then just taking the record with the highest record ID doesn't always do the job (if there is more than one AOS, which should always be the case in production).

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: How to get only last record in table by using query in AX 2012

    I think, Rahul means the last record in the table (bigest RecId), not the last record in the query.

    By the way, if you have a createdDateTime-style field for sorting, or a field from a number sequences (continuous), I recommend using it.

  • Martin Dráb Profile Picture
    233,326 Most Valuable Professional on at
    RE: How to get only last record in table by using query in AX 2012

    No, I did mean Ascending. If we want to to get the last record when using descending sort order (as in the question), we can use FirstOnly and use the opposite sort order (therefore we need to change Descending to Ascending).

    If we just take the first record instead of the last, we would get different record than required.

  • André Arnaud de Calavon Profile Picture
    294,715 Super User 2025 Season 1 on at
    RE: How to get only last record in table by using query in AX 2012

    Small correction on the answer from Martin. It should be set to "descending". www.differencebetween.com/.../

    I think Martin intended to use descending as well. He might typed too quickly.

  • Suggested answer
    nmaenpaa Profile Picture
    101,158 Moderator on at
    RE: How to get only last record in table by using query in AX 2012

    As Martin told you, you can use firstOnly to get only the first record that matches your criteria. Then all you need to do is to adjust your criteria so that the "last" is first.

  • Suggested answer
    Martin Dráb Profile Picture
    233,326 Most Valuable Professional on at
    RE: How to get only last record in table by using query in AX 2012

    Set query.firstOnly1(true) to get just the first record.

    And change sort direction to SortOrder::Ascending, therefore the last record will become the first.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 225

#2
Martin Dráb Profile Picture

Martin Dráb 166 Most Valuable Professional

#3
Adis Profile Picture

Adis 131 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans