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)
{
    A_PurchaseOrder objA_PurchaseOrder;
    int records;
    info("------------------START-------------------");
    while select objA_PurchaseOrder
    order by Purchase_ID desc
    {
       info("--------------NEW RECORD--------------");
       info (strfmt("Purchase_ID: %1", objA_PurchaseOrder.Purchase_ID));
       info (strfmt("Vender_Code: %1", objA_PurchaseOrder.Vender_Code));
       info (strfmt("Purchase_Date: %1", objA_PurchaseOrder.Purchase_Date));
       info (strfmt("Status: %1", objA_PurchaseOrder.Status));
       info (strfmt("Purchase_Amount: %1", objA_PurchaseOrder.Purchase_Amount));
       records++;
    }
    info("------------------END-------------------");
    info(strfmt("%1 records was selected", records));
}


Related Posts

Previous
Next Post »

Thanks for comments.....