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.
Custom Ax table.
Following a simple code snippet which read csv file and insert data into custom data
staticvoid 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.
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