General X++ Development

How do I get the first matching record in X++?

Asked by Sunny Vasty· Aug 13, 2026· 1 answers

How do I get the first matching record in X++?

SSunny Vasty75 reputation · answered Aug 13, 2026

Use firstOnly. If "first" means newest, oldest, highest, or lowest, pair it with order by.

X++
SalesTable salesTable;

select firstOnly salesTable
    order by salesTable.CreatedDateTime desc
    where salesTable.CustAccount == 'US-001';

Without an order by, "first" should not be treated as a business rule. If the order matters, make it explicit.