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, ...
Answered

x++ query - sort by summed field

(0) ShareShare
ReportReport
Posted on by 5

Hello!

I'm trying to sort my query by the summed field.

It seems like the query does sum the fields, but sorting is not applied.

Here is my code.

Query q;
QueryRun qr;
QueryBuildDataSource qbd;
CustInvoiceTrans tt;
int i = 0;

q = new Query();

qbd = q.addDataSource(tableNum(CustInvoiceTrans));
qbd.addGroupByField(fieldNum(CustInvoiceTrans, ItemId));
qbd.addSelectionField(fieldNum(CustInvoiceTrans,Qty),SelectionField::Sum);
qbd.addSortField(fieldNum(CustInvoiceTrans,Qty),SortOrder::Descending);

qr = new QueryRun(q);

//For testing the query
while(qr.next()){
    tt = qr.get(tableNum(CustInvoiceTrans));
    info(tt.ItemId   " - "   int2str(tt.Qty));

    i  ;
    if(i > 5) break;
}

I also tried using QueryBuildFieldList, but it was still unsuccessful in sorting the table.

Query q;
QueryRun qr;
QueryBuildDataSource qbd;
QueryBuildFieldList qbfl;
CustInvoiceTrans tt;
int i = 0;

q = new Query();

qbd = q.addDataSource(TableNum(CustInvoiceTrans));
qbfl = qbd.fields();
qbfl.addField(fieldNum(CustInvoiceTrans, ItemId));
qbfl.addField(fieldNum(CustInvoiceTrans, Qty), SelectionField::Sum);
qbd.addGroupByField(fieldNum(CustInvoiceTrans, ItemId));
qbd.addSortField(fieldNum(CustInvoiceTrans, Qty), SortOrder::Descending);

qr = new QueryRun(q);
while(qr.next()){
    tt = qr.get(tableNum(CustInvoiceTrans));
    info(tt.ItemId   " - "   int2str(tt.Qty));

    i  ;
    if(i > 5) break;
}

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

    Your code sorts by Qty, not by SUM(Qty). Therefore it won't give you what you want. And you can't order by functions in AX queries.

    But it doesn't mean that you can't achieve your goal. Summarize the data in a view and then sort the view.

  • eilev brustugun Profile Picture
    5 on at

    Thanks Martin.

    I have created a view that sorts by the summed field, and it works.

    The only problem is that later i probably want to filter the lines by a date range, which means i have to filter the data before applying the sum function. Is this possible to do with views?

  • Verified answer
    Martin Dráb Profile Picture
    239,636 Most Valuable Professional on at

    No, it's not possible.

    Then the alternative is putting aggregated data to a temporary table and sorting this table.

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 797

#2
André Arnaud de Calavon Profile Picture

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

#3
Subra Profile Picture

Subra 534

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans