AYETY
AYETY

Reputation: 710

How to count and sort records using axapta query?

I want to count how many records are there in CustTable (group by CustGroup) and sort the number of records in each custgroup descending using query code.

here is my code:

q = new Query();
queryBuildDataSource = q.addDataSource(tableNum(custTable));
queryBuildDataSource.addGroupByField(fieldNum(custTable, CustGroup));
//queryBuildDataSource.addSortField(fieldNum(custTable, count(RecId), SortOrder::Descending));
qr = new QueryRun(q);
while(qr.next())
{
   custTable = qr.get(tableNum(custTable));
   info(strFmt("%1 --- %2", custTable.CustGroup, custTable.RecId));
}

I know that 'count' does not work here... how can I solve it?

Upvotes: 1

Views: 5736

Answers (1)

Jan B. Kjeldsen
Jan B. Kjeldsen

Reputation: 18051

You cannot (in X++) sort on an aggregated field.

What you can do is make a view, then sort on its output explained in this answer and here.

Upvotes: 2

Related Questions