X++ load operating units for financial dimensions CSV import


static void LoadDepartmentCost(Args _args)
{

    #File
    OMOperatingUnit ou,ou2;
    OMOperatingUnitType t;
    RecId omOpRecId;
    NumberSeq num;
    IO  iO;
    FilenameOpen        filename;// = "c:\\share\\test.csv";//To assign file name
    Container           record;
    str type;
    int counter = 5, alreadyExists = 0;

    Dialog          dialog;
    DialogField     dialogfileName;


    //dialog to load the excel file of contact information
        dialog = new Dialog("Upload File");

        dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));

        dialog.filenameLookupFilter(["All files",#AllFiles]);
        //dialog.filenameLookupTitle("Employee Data");
        dialog.caption("Upload");

    if(dialog.run())
    {
       filename = dialogFilename.value();
    }

    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)
        {
            omOpRecId = OMOperatingUnit::findOMOperatingUnitNumber(conpeek(record, 3)).RecId;

            ttsBegin;

            select forUpdate ou where ou.RecId == omOpRecId;

            ou.OMOperatingUnitType = str2enum(t, conpeek(record, 1));
            ou.OMOperatingUnitNumber = conpeek(record, 3);
            ou.Name =  conpeek(record, 2);
            ou.NameAlias = conpeek(record,2);



            if(ou.RecId)
            {
                alreadyExists ++;


                if(ou.validateWrite())
                {
                    ou.update();
                }
            }
            else
            {
                info(strFmt("%1 - %2", ou.OMOperatingUnitNumber, ou.OMOperatingUnitType));
                ou.PartyNumber = DirPartyTable::getNewPartyNumber();

                if(ou.validateWrite())
                {
                    ou.insert();

                }
            }
            ttsCommit;

        }


    }
    info(strFmt("%1",alreadyExists));
}

Comments

Popular posts from this blog

AX 2012 DEVOPS Build, Release setup and configuration

AX 2012 Build process using AZURE DEVOPS

What is Microsoft Power Platform?