AX 2012 - Import data from .csv file

Today we will learn on how to import data from a .csv file into AX 2012 through a Job

1)     Create a table TEC_Import
Emp_Age        int
Emp_Dob        date
Emp_Id           str 20
Emp_Name     str 20

2)     Write a job

static void Import(Args _args)
{
    #File
    IO              iO;
    TEC_Import      import;
    FilenameOpen    filename = "c:\\import.csv";//To assign file name
    Container       record;
    boolean         first = true;
    boolean         ifexist = false;
    TEC_ID          empid;
    ;
    iO = new CommaTextIo(filename,#IO_Read);
    if (! iO || iO.status() != IO_Status::Ok)
    {
        throw error("@SYS19358");
    }
     ttsBegin;
    while (iO.status() == IO_Status::Ok)
    {

        record = iO.read();// To read file
        if (record)
        {
            if (first)  //To skip header
            {
                first = false;
            }
            else
            {

                empid = conPeek(record, 1);
                select Emp_Id from import where import.Emp_Id == empid;
               /*if(import.Emp_Id)
                {
                first = false; //skip those records whose id's are same
                info("Record already exist");

                }
                else
                {*/
                    //  read container to insert record in your table…….
                import.Emp_Id = conPeek(record, 1);
                import.Emp_Name = conPeek(record, 2);
                import.Emp_Age = str2int(conPeek(record, 3));
                import.Emp_Dob = str2Date(conPeek(record, 4),123);
                    //if(import.validateWrite())
                    //{
                         import.insert();

                      //   info("Inserted successfully");
                    //}
                    //else
                    //{
                      //  info("Insertion Failed due to exception");
                   // }

                //}

            }
        }
       }
               ttsCommit;
}


No comments:

Post a Comment