≡ Menu




Reading CSV files in Dynamics Ax 2012.

Today I come across a scenario where, I have to read data from csv file.

 

Reading data from csv file is very easy with IO operation. For example I have csv file with following student first name and last name which I have to migrate into custom ax table.

CVS file

 

 

Custom Ax table.

StudentInfoTable

 

 

Following a simple code snippet which read csv file and insert data into custom data

static void Job9(Args _args)

{

#File

IO  iO;

Name FirstName;

Name LastName;

StudentInfo _StudentInfo;

FilenameOpen        filename = “c:\\StudentInfo.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

{

 

FirstName = conpeek(record, 1);//To peek record

LastName = conpeek(record, 2);

_StudentInfo.FirstName=FirstName;

_StudentInfo.LastName =LastName;

_StudentInfo.insert();

//     info(strfmt(‘%1–%2’,custAccount,custname));

}

}

}

}

 

 

After running the ax job, I found following data into Ax table.

4-28-2015 9-59-45 PM

 

If you saw, first row of csv is missing in table. The reason for that above code snippet consider first row as header so it did not require to insert it.

 

One thing I missed, The reference, original code snippet is belongs to “Jitendra Kumar Singh”

http://axaptacorner.blogspot.com/2012/09/how-to-read-csv-files-in-ax-2012.html

{ 0 comments… add one }

Leave a Comment