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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

X++ query bug with order by

(2) ShareShare
ReportReport
Posted on by 11
An unpleasant discovery with some inherited code today.
 
This block of code does not order results as expected because the field used does not exist, it does however compile without issue.
str lineDisc = 'Disc.';
date curDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

PriceDiscAdmTable priceDiscAdmTable;
PriceDiscAdmTrans priceDiscAdmTrans;

while
	select priceDiscAdmTable
		order by priceDiscAdmTable.ModifiedDateTime desc // ***THIS FIELD DOES NOT EXIST***
		where (PriceDiscAdmTable.Posted == 1)
	join priceDiscAdmTrans
		where (priceDiscAdmTrans.JournalNum == priceDiscAdmTable.JournalNum) && (priceDiscAdmTrans.ItemRelation == lineDisc) && (PriceDiscAdmTrans.FromDate <= curDate) && (PriceDiscAdmTrans.ToDate >= curDate)
{
	discounts.addEnd(priceDiscAdmTrans);
}
 
the intended code
 
str lineDisc = 'Disc.';
date curDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

PriceDiscAdmTable priceDiscAdmTable;
PriceDiscAdmTrans priceDiscAdmTrans;

while
	select priceDiscAdmTable
		where (PriceDiscAdmTable.Posted == 1)
	join priceDiscAdmTrans
		order by priceDiscAdmTrans.ModifiedDateTime desc // ***THIS FIELD DOES EXIST***
		where (priceDiscAdmTrans.JournalNum == priceDiscAdmTable.JournalNum) && (priceDiscAdmTrans.ItemRelation == lineDisc) && (PriceDiscAdmTrans.FromDate <= curDate) && (PriceDiscAdmTrans.ToDate >= curDate)
{
	discounts.addEnd(priceDiscAdmTrans);
}
 
Categories:
I have the same question (0)
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,495 Super User 2026 Season 1 on at
     
    The correct sequence of code 
    str lineDisc = 'Disc.';
    date curDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
    PriceDiscAdmTable priceDiscAdmTable;

    PriceDiscAdmTrans priceDiscAdmTrans;

    // Container or list to hold the results 
    List discounts = new List(Types::String); // or whatever collection you're using

    while select priceDiscAdmTable
            where priceDiscAdmTable.Posted == 1
             join priceDiscAdmTrans
             order by priceDiscAdmTrans.ModifiedDateTime desc
                     where priceDiscAdmTrans.JournalNum == priceDiscAdmTable.JournalNum
                     && priceDiscAdmTrans.ItemRelation == lineDisc
                     && priceDiscAdmTrans.FromDate <= curDate
                     && priceDiscAdmTrans.ToDate >= curDate
    {
    discounts.addEnd(priceDiscAdmTrans);
    }

    Reason why the second approach works is the order by comes after the join keyword and the X++ parser associates it with the joined table priceDiscAdmTrans.

    Alternatively, would recommend using a query object in this case.

    Query query = new Query();
    QueryBuildDataSource qbdsHeader,qbdsLine;
    QueryRun qr;

    //Header
    qbdsHeader = query.addDataSource(tablenum(PriceDiscAdmTable));
    qbdsHeader.addRange(fieldnum(PriceDiscAdmTable,Posted)).value(queryvalue(1));

    //PriceDisc Adm lines
    qbdsLine = qbdsHeader.addDataSource(tablenum(PriceDiscAdmTrans));
    qbdsLine.relations(true);
    qbdsLine.joinMode(JoinMode::InnerJoin);
    qbdsLine.addRange(fieldnum(PriceDiscAdmTrans,ItemRelation)).value(lineDisc);
    qbdsLine.addRange(fieldnum(PriceDiscAdmTrans,FromDate)).valueLessThanDate(curDate);
    qbdsLine.addRange(fieldnum(PriceDiscAdmTrans,ToDate)).valueDateGreaterThan(curDate);

    //Sort by line ModifiedDateTime
    qbdsLine.addSortField(fieldnum(PriceDiscAdmTrans,ModifiedDateTime),SortOrder::Descending);

    qr = new QueryRun(query);

    while(qr.next())
    {
           priceDiscAdmTrans = qr.get(tablenum(priceDiscAdmTrans));
           discounts.addEnd(priceDiscAdmTrans);

    }

    Hope this helps. Happy to answer questions, if any.

     

  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at
    What is the question? It seems that you found a bug in the code and tried to fix it, so the purpose of this thread isn't clear to me.
     
    Do you mean that you have another problem with your new code that you forgot to mention?
  • André Arnaud de Calavon Profile Picture
    303,269 Super User 2026 Season 1 on at
    Hi rvoisin,
     
    If you have a question, please update the thread. If so, can you also indicate if the coding you shared is part of a customization, ISV/partner solution or the standard application?
    Also, please clarify the product as you now used two conflicting tags: Dynamics AX is referring to older products where Dynamics 365 Finance is the current supported product.
  • rvoisin Profile Picture
    11 on at
    This is effectively a bug report against X++, I'm not looking for a solution. There does not seem to be an official place to drop those.
     
    The first sequence shown should not compile, but does. When that sequence is used, the order by is silently dropped and results are given in the default DB ordering.
     
    The example shown is a simplified part of a customization that wasn't giving the expected answers, the query isn't optimal.
     
    @Navneeth Nagrajan I've put some time into comparing select/Query/SysDa methods, and outside of some corner-cases where you need to modify queries at runtime, using select results in faster queries, and much more readable code. SysDa methods are comparable in speed to select, harder to write/test, and VERY poorly documented. Query objects tend to generate the slowest queries out of the 3.
  • Suggested answer
    Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at
    This is a community forum where you can discuss things with your peers, not a place to report bugs to Microsoft. In this case, I suggest to log a suggestion at Ideas (you can get there by clicking Get involved > Submit ideas on the top of this page).
     
    It would be nice if the compiler took into account whether a table has Modified Date Time property set to Yes or No, but it's not the case. The compiler doesn't know whether the ModifiedDateTime field is actually used or not. For example, this is code compiles too despite the fact that CustGroup table has Modified Date Time = No.
     CustGroup cg;
     var x = cg.ModifiedDateTime;
    

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 545 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 408

#3
Adis Profile Picture

Adis 267 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans