Friday, July 28, 2017

How to use sorting in the select statements in Dynamics Ax

By default, a while select statement that returns multiple rows will sort the result ascending by the primary index on the table. You can see this in the first two examples in the previous section.
If you would like to have a statement return the rows in a different order, you have to use the order by parameter in the select statement and specify which fields you would like to sort the result by. If you have an index that corresponds with the sorting, you can use the name of the index to order by as well, but then you will have to use the index statement instead of order by. The following example will return all the records in the CarTablesorted in a descending order of mileage:

static void selectRecordsSortedDesc(Args _args)
{
    CustTable custtable;
    int records;
    info("------------------START-------------------");
    while select custtable
        order by AccountNum desc
    {
       info("--------------NEW RECORD--------------");
       info (strfmt("AccountNum: %1",custtable.AccountNum));
       info (strfmt("Name: %1",custtable.name()));
       info (strfmt("CustGroup: %1",custtable.CustGroup));
       info (strfmt("Currency: %1",custtable.Currency));
     
       records++;
    }
    info("------------------END-------------------");
    info(strfmt("%1 records was selected", records));
}

No comments:

Post a Comment

D365 Extensions: Extend the validateField Method on a Table

In this post I will going to show you, how to extend the validateField method on a table. As example I used the SalesLine, where I will ...