Translate

Thursday, August 4, 2011

Writing Data to CSV file from Dyanamics AX

Here is a code to write date from Microsoft Dyanics Ax to comma separated file(.csv).

static void AX2CSV(Args _args)
{
    CommaTextIO file;
    container line;
    InventTable invenTable;
    #define.filename('u:\\items.CSV')
    #File
    ;
    file = new CommaTextIO(#filename, #io_write);
    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    file.outFieldDelimiter(';'); // for semicolon seperator and default field delimitter is ,(comma)
    while select invenTable
    {
        line = [invenTable.ItemId,invenTable.ItemName,invenTable.ItemGroupId];
        file.writeExp(line);
    }
}
Good Luck.. :)

Monday, August 1, 2011

Reading a CSV file In Dynamics AX

Solution to read CSV file data to AX.
NS: you can set a delimitter to any charachter in InFielddelimitter() Method.

static void CSVtoAX(Args _args)
{
    #File
    CommaTextIo        commaTextIo;
    FileIOPermission   permission;
    container          containFromRead;
    int                x;
    int                cols;
    ;
    commaTextIo = new CommaTextIO('u:\\items.csv',#io_read);
    commaTextIo.inFieldDelimiter(';');
    containFromRead = commaTextIo.read();
    While(containFromRead)
    {
        cols = conLen(containFromRead);
        for(x=1;x<=cols;x++)
        {
            print conpeek(containFromRead,x);
        }
        containFromRead = commaTextIo.read();
    }
    pause;
    commaTextIo = null;
}