AX- Export Data to CSV via X++ Query

AX- Export Data to CSV via X++ Query

static void ExportDataToCSV(Args _args)
{
Query q;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
QueryRun qr;
CommaIO commaIO;
FileName fileName;
InventTable inventTable;
;
fileName = "C:\\Users\\Contoso\\Desktop\\" + "ItemDetail" + ".csv"; //Destination
commaIO = new CommaIO(fileName,'W');
q = new Query();
qbds = q.addDataSource(tablenum(InventTable));
qbr = qbds.addRange(fieldnum(InventTable,ItemId)); //Query Range
qr = new QueryRun(q);
commaIO.write("ItemId","Item Type"); //Header
//Loop to insert values in the csv from the table
while( qr.next() )
{
inventTable = qr.get(tablenum(InventTable));
commaIO.write(inventTable.ItemId,enum2str(inventTable.ItemType));
}
WINAPI::shellExecute(fileName);
}


OR

static void ShyamTabletoCSVFile(Args _args)
{
 CommaTextIo file;
container line;
CustTable custTable;
#define.filename(@'C:\Temp\Testing\TestFile.csv')
#File
file = new CommaTextIo(#filename, #io_write);
if (!file || file.status() != IO_Status::Ok)
{
throw error("File cannot be opened.");
}
while select * from custTable
{
line = [custTable.AccountNum ,custTable.Name()];
file.writeExp(line);
}
info(strFmt("File %1 created in specified folder.", #filename));
}

Comments