D365FO X++: Why `firstOnly` can still return the wrong row unless you make the ordering explicit
`firstOnly` is a common shortcut in D365FO, but it does not by itself make the returned row deterministic. This article shows how to combine `firstOnly`, `order by`, and the right query shape so you get the record you actually meant to fetch.
D365FO X++: Why firstOnly can still return the wrong row unless you make the ordering explicit#
A lot of D365FO bugs start with a statement that looks harmless:
select firstonly myTable
where myTable.SomeField == someValue;The problem is that firstOnly only says “give me one row”, not “give me the row I expect.” In X++, the platform documentation is explicit that firstOnly does not guarantee a maximum of one row in every execution path, and the returned row depends on how the database and kernel satisfy the query. If you care which row comes back, you need to make the ordering explicit. (learn.microsoft.com)
When would you use this?#
Use this pattern when you are fetching a single record and the business meaning depends on which one you get:
- newest transaction
- oldest open record
- highest or lowest sequence
- the “top” record by a business date or a priority field
If the order does not matter, firstOnly is fine as a convenience. If the order does matter, relying on the default execution order is a bug waiting to happen. (learn.microsoft.com)
The common mistake#
Developers often assume this means “the first matching row in the table”:
select firstonly custTable
where custTable.CustGroup == custGroup;That is not a safe assumption. Microsoft’s X++ syntax documentation explains that firstOnly is not a determinism guarantee, and the select statement documentation says to use order by when order matters. (learn.microsoft.com)
So if you want the latest customer by creation time, or the highest value by a sequence field, you must say so in the query.
Recommended pattern#
Add an order by clause that matches the record you want:
CustTable custTable;
select firstonly custTable
order by CreatedDateTime desc
where custTable.CustGroup == custGroup;If you need the oldest row instead, remove desc. If you need a business-specific priority, order by that field first and add a tie-breaker field after it. The select statement documentation also notes that you can combine order by with index hint when you want to align the query with a known index. (learn.microsoft.com)
Why this works better#
firstOnly limits the intent to a single result, but order by defines which row should win. Without ordering, the database is free to return any matching row that satisfies the query. That means the same code can appear to work in testing and then return a different row later when the data distribution changes or the query plan changes. This is exactly the kind of issue that shows up as “random” behavior in production. (learn.microsoft.com)
A practical example#
Say you need the most recent approved sales order for a customer:
SalesTable salesTable;
select firstonly salesTable
order by CreatedDateTime desc
where salesTable.CustAccount == custAccount
&& salesTable.SalesStatus == SalesStatus::Backorder;If your business rule is actually “latest by approval date”, then order by the approval date field, not CreatedDateTime. The important part is that the sort field must match the business meaning, not just the table’s physical storage order. (learn.microsoft.com)
Don’t confuse firstOnly with traversal#
If you want to walk multiple rows, use while select or next, not firstOnly. Microsoft’s select documentation says next is for iterating after an initial select and that firstOnly should not be mixed with that pattern. (learn.microsoft.com)
For example:
CustTable custTable;
while select custTable
order by AccountNum
{
// process each row
}That is a different problem from “give me one specific row.” If you only need one row, keep the query single-row and make the ordering explicit.
Things to watch for#
1) Add a tie-breaker when the sort field is not unique#
If two records can share the same sort value, the result can still be ambiguous unless you add another field to break the tie.
2) Use the right field for the business rule#
Sorting by RecId or CreatedDateTime is not automatically correct. It is only correct if the business meaning really is “latest inserted” or “latest created.”
3) Be careful with cached or optimized reads#
Microsoft notes that query execution can be affected by caching behavior, and the returned result can depend on how the platform satisfies the statement. That is another reason to make the query deterministic instead of relying on default row order. (learn.microsoft.com)
4) Verify the SQL if the result still looks odd#
If the code still behaves unexpectedly, use Trace Parser to inspect the executed query and confirm the generated SQL and call path. Microsoft’s Trace Parser documentation shows that it can display database operations and X++ call trees, which is useful when you need to confirm what the runtime actually did. (learn.microsoft.com)
Recommended approach#
My default pattern is:
- Decide exactly which row the business rule wants.
- Put that rule into
order by. - Add a second sort field if ties are possible.
- Keep
firstOnlyonly when you really want a single row. - Use Trace Parser if the runtime behavior still does not match what you expected. (learn.microsoft.com)
Conclusion#
firstOnly is not the same thing as “the correct row.” In D365FO X++, if the row matters, define the order. That small change usually turns a flaky query into a stable one.
Sources#
- X++ syntax - Finance & Operations | Dynamics 365 | Microsoft Learn — Microsoft Learn
- Select statement - Finance & Operations | Dynamics 365 | Microsoft Learn — Microsoft Learn
- Select data - Finance & Operations | Dynamics 365 | Microsoft Learn — Microsoft Learn
- Diagnose issues and analyze performance by using Trace parser - Finance & Operations | Dynamics 365 | Microsoft Learn — Microsoft Learn
- Diagnose issues and analyze performance using a trace parser with the unified developer experience. — Microsoft Learn
Missing something?
Suggest a topic you'd like to see covered next. The most requested topics help shape future XppForge documentation.
Sign in to suggest a topic