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 :
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 632

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

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    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.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    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.

  • André Arnaud de Calavon Profile Picture
    301,020 Super User 2025 Season 2 on at

    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.

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    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.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    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
    237,878 Most Valuable Professional on at

    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,826 Moderator on at

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

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans