Thursday, September 20, 2012

How to read CSV files in AX 2012 through X++ code


Through following code we can read CSV files

static void ReadCsvFile(Args _args)
{
    #File
    IO  iO;
    CustAccount custAccount;
    CustName custname;
    FilenameOpen        filename = "d:\\jit.csv";//To assign file name
    Container           record;
    boolean first = true;
    ;
    iO = new CommaTextIo(filename,#IO_Read);
    if (! iO || iO.status() != IO_Status::Ok)
    {
        throw error("@SYS19358");
    }
    while (iO.status() == IO_Status::Ok)
    {
        record = iO.read();// To read file
        if (record)
        {
            if (first)  //To skip header
            {
                first = false;
            }
            else
            {
             
                custAccount = conpeek(record, 1);//To peek record
                custname = conpeek(record, 2);
                info(strfmt('%1--%2',custAccount,custname));
            }
        }
    }
}

Happy X++ coding!!!

1 comment:

  1. in this code how can I check existing records that should not be inserted from .csv file

    ReplyDelete