CSV File operation in Dynamic AX

Rumman Ansari   Software Engineer   2019-10-01   6191 Share
☰ Table of Contents

Table of Content:


Question

1. Create a table in AX. The name of the table will be Table 1.

The table need to contain the below data.

SO_ID Name
1 Rumman Ansari

2. Create a csv file. Name of the csv file is info.csv

csv file will be like this

info.csv

2,Raja
3,Rumman Ansari
4,Mohon

Task 1: You have to take data from the csv file and have to put those data in the table.

Task 2: Create a JOB for this.

Task 3: After that you have to update the data where all the name present as Rumman Ansari to another name Rambo

Solution:

To do this we will create two different Job. In first job will will insert all the data from the csv file to the table. In the second job we will update the name form Rumman Ansari to Rambo.

Job 1


static void Job1(Args _args)
{
#File
IO  iO;
int SO_ID;
str Name;
Table1 _table1;
FilenameOpen        filename = @"C:\Users\ansari\Desktop\info.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 { 

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

    Name = conpeek(record, 2);

    _table1.SO_ID= SO_ID;

    _table1.Name = Name;
    _table1.insert();  
     info("File inserted in the Table");
    }

  }
 }
}

Job 2


static void Job2UpdateTableData(Args _args)
{
     // Create table buffer
    Table1 table1Buffer;    
      
      // the above and this code update the all the rows
        update_recordset table1Buffer setting
            Name="Rambo"
            where table1Buffer.Name == "Rumman Ansari";
            info("All Data Updated");
    
}

Test case based on the above example

You have a table order, which contains two fields like a) SO_ID b) status. We have to change the status based on the SO_ID. We will have a CSV file which will contain the SO_ID and status. We have to take that SO_ID and have to change the status pending to verified.

We have a table like below.

SO_ID status
111 pending

Task You have to change the status using a CSV file, where the SO_ID and status will be given like below.

info.csv

SO_ID,status
2,verified

static void CSVJob(Args _args)
{
#File
IO  iO;
int SO_ID;
str status; 
Table3 _table1;
FilenameOpen filename = @"C:\Users\ansarir2\Desktop\info.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 {

             SO_ID = conpeek(record, 1);//To peek record
            status = conpeek(record, 2);

              info(strFmt(" Found Id: %1 Status: %2",SO_ID,status)) ;
            
            // the above and this code update the all the rows 
    
      ttsbegin;
            select forupdate _table1 where _table1.SO_ID == SO_ID;
            _table1.Name = status;
            _table1.update();
        ttscommit;
        info("Status Updated");
            }

        }
    
   
 } 
    
}

Output of the code will be below

SO_ID status
111 verified