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;
}

Writing date to Txt file

Here is a solution for writing a date to Text file from Dynamics Ax.
Before running this code a physical file should be created as specified in the code('u:\\items.txt')

static void DataToTxtFile(Args _args)
{
    TextIO file;
    container line;
    InventTable invenTable;
    #define.filename('u:\\items.txt')
    #File
    ;
    file = new TextIO(#filename, #io_write);
    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    file.outFieldDelimiter(';');// for semicolon seperator
    while select invenTable 
    {
        line = [invenTable.ItemId,invenTable.ItemName,invenTable.ItemGroupId];
        file.writeExp(line);
    }
}