Interview Questions

Thursday, October 16, 2014

Exporting the Data from AX to CSV file

Hi All,

Today I want to share a code for exporting data from AX to CSV file with an example of Invent table.

static void ExportDataToCSV(Args _args)
{
    Query                                 q;
    QueryBuildDataSource     qbds;
    QueryBuildRange             qbr;
    QueryRun                         qr;
    CommaIO                         commaIO;
    FileName                          fileName;
    InventTable                      inventTable;
    ;
   
    fileName       = WINAPI::getTempPath() + "ItemDetails" + ".csv";
    commaIO      = new CommaIO(fileName,'CSVFile');
   
    q                  = new Query();
    qbds             = q.addDataSource(tablenum(InventTable));
    qbr               = qbds.addRange(fieldnum(InventTable,ItemId));
   
    qr                = new QueryRun(q);
   
    commaIO.write("ItemId","Item Name","Item Type","Item GroupId","Dimension GroupId","Model GroupId");
    while( qr.next() )
    {
        inventTable = qr.get(tablenum(InventTable));
       
        commaIO.write(inventTable.ItemId,inventTable.ItemName,enum2str(inventTable.ItemType),inventTable.ItemGroupId,
                      inventTable.DimGroupId,inventTable.ModelGroupId);
    }
   
    WINAPI::shellExecute(fileName);
}