Skip to main content

Code to import Employee Master data in ax 2012 R3 x++

Hello ,

Here I am sharing with you the code to import the Employee Master data in ax 2012 R3.
I have used this code for one of my Saudi Arabian client so some line would be new for you. Customize the code as per your requirement.

Hope it will help you thanks :)

------------------------------------------------------------------------------------------------------------

static void ImportWorkerthruExcel(Args _args)
{
    SysExcelApplication xlsApplication;
    SysExcelWorkBooks xlsWorkBookCollection;
    SysExcelWorkBook xlsWorkBook;
    SysExcelWorksheets xlsWorkSheetCollection;
    SysExcelWorksheet xlsWorkSheet;
    SysExcelRange xlsRange;
    SysExcelCells Cells;
    SysExcelCell RCell;
    CommaIO inFile;
    int nRow,i;
    DialogField dialogPath;
    Dialog dialog;
    Filename filename;
    Name firstName, LastName;
    LegalEntity LegalEntity;
    HcmPersonnelNumberId PersonnelNo;
    HcmEmploymentType WorkerType;
    HcmWorker hcmWorker;
    DirPerson dirPerson;
    DirPartyTable DirPartyTable;
    HcmEmployment HcmEmployment;
    DirPersonName DirPersonName;
    HcmPersonPrivateDetails HcmPersonPrivateDetails;
    HcmPersonIdentificationNumber HcmPersonIdentificationNumber;
    HcmWorkerTitle      hcmWorkertitle;
    str             WorkLocation;

    // Variabless for Number Sequence
    NumberSequenceReference             numberSeqReferencePersonnelNum;
    NumberSequenceTable                 numberSeqTablePersonnelNum;
    NumberSeq                           numberSeqPersonnelNum;
    NumberSequenceReference             numberSeqReferencePersonnelActionNum;
    NumberSequenceTable                 numberSeqTablePersonnelActionNum;
    NumberSeq                           numberSeqPersonnelActionNum;
    Num                                 lastNumber;
    Num                                 lastPersonnelActionNumber;
    SysLastValue nextNumSeq;
    SysLastValue nextPersonnelActionNumSeq;

    // Variables for number Sequence
   // importworker importworker;
    NameAlias SearchName;
    BirthDate   birthdate;
    str         CountryCode;
    str         IdentificationNumber;
    ExpirationDate  ExpireDate;



    DirAddressBookPartyAllView DirAddressBookPartyAllView;
    HcmPositionDuration HcmPositionDuration;
    ;
    dialog = new Dialog("Import");
    dialogPath = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();
    if (dialog.run())
    {
    filename = (dialogPath.value());
    }
    inFile = new CommaIO (filename, 'R');
    if (!inFile || infile.status() != IO_Status::Ok )
    {
    throw error (strfmt("@SYS19312",filename));
    }
    try
    {
    xlsApplication = SysExcelApplication::construct();
    xlsWorkBookCollection = xlsApplication.workbooks();
    xlsWorkBookCollection.open(filename);
    xlsWorkSheetCollection = xlsApplication.worksheets();
    xlsWorkSheet = xlsWorkSheetCollection.itemFromNum(1);
    Cells = xlsWorkSheet.Cells();
    nRow = 2;
    RCell = Cells.Item(nRow, 2);

    while(RCell.value().bstr() != "")
    {
    // code for Personnel number
    numberSeqReferencePersonnelNum  = NumberSeqReference::findReference(extendedTypeNum(HcmPersonnelNumberId));
    numberSeqTablePersonnelNum      = NumberSequenceTable::find(numberSeqReferencePersonnelNum.NumberSequenceId);

    if (numberSeqTablePersonnelNum)
    {
        // generate worker personnel number if number sequence code is created.
        numberSeqPersonnelNum = NumberSeq::newGetNumFromId(numberSeqTablePersonnelNum.RecId,
                                                           true,
                                                           true,
                                                           nextNumSeq,
                                                           true);

        if(numberSeqPersonnelNum)
        {
            [lastNumber] = nextNumSeq.Value;
           // personnelNumber.text(lastNumber);
          PersonnelNo = lastNumber;
        }

       // personnelNumber.allowEdit(numberSeqTablePersonnelNum.numAllowEdit());
    }
        // Code for Personneel Number

    RCell = Cells.item(nRow,2);
    SearchName = RCell.value().bStr();
    //RCell = Cells.item(nRow,2);
    //PersonnelNo = RCell.value().bStr();
    RCell = Cells.item(nRow,3);
    FirstName = RCell.value().bStr();
    RCell = Cells.item(nRow,4);
    LastName = RCell.value().bStr();

    RCell = Cells.item(nRow,13);
    birthdate = RCell.value().date();

    RCell = Cells.item(nRow,5);
    CountryCode = RCell.value().bStr();

    RCell = Cells.item(nRow,7);
    WorkLocation = RCell.value().bStr();


    DirPerson.Name = firstname+" "+LastName;
    DirPerson.NameSequence = 5637144576;
    DirPerson.LanguageId = "en-us";
    dirperson.NameAlias = SearchName;
    DirPerson.insert();

    DirPartyTable.RecId=dirPerson.RecId;
    DirPartyTable.insert();

    dirpersonname.FirstName =  firstname;
    dirpersonname.LastName  = LastName;
    DirPersonName.Person    = dirPerson.RecId;
    DirPersonName.ValidFrom = DateTimeUtil::newDateTime(today(),0);
    DirPersonName.ValidTo   = DateTimeUtil::maxValue();
    dirpersonname.insert();

    hcmWorker.PersonnelNumber = PersonnelNo;
    hcmWorker.Person=dirPerson.RecId;
    hcmWorker.insert();

    HcmWorkerTitle.OfficeLocation = WorkLocation;
    HcmWorkerTitle.ValidFrom      = DateTimeUtil::newDateTime(today(),0);
    HcmWorkerTitle.ValidTo        = DateTimeUtil::maxValue();
    HcmWorkerTitle.Worker         = hcmWorker.RecId;
    HcmWorkerTitle.insert();

        // Hcm Employement Table Data
    HcmEmployment.EmploymentType = HcmEmploymentType::Employee;
    HcmEmployment.LegalEntity = CompanyInfo::find().RecId;
    HcmEmployment.ValidFrom = DateTimeUtil::newDateTime(today(),0);
    HcmEmployment.ValidTo = DateTimeUtil::maxValue();
    HcmEmployment.Worker=hcmWorker.RecId;
    HcmEmployment.insert();

        // Worker Private Details (HCMPersonPrivateDetails)
    HcmPersonPrivateDetails.BirthDate = birthdate;
    HcmPersonPrivateDetails.CitizenshipCountryRegion = CountryCode;
    HcmPersonPrivateDetails.Person = dirPerson.RecId;
    HcmPersonPrivateDetails.insert();

    // Worker Identification Numbers
    RCell = Cells.item(nRow,10);
    if(RCell.value().bStr() != "")
    {
        HcmPersonIdentificationNumber.IdentificationNumber = RCell.value().bStr();
        RCell = Cells.item(nRow,11);
        HcmPersonIdentificationNumber.ExpirationDate = RCell.value().date();
        HcmPersonIdentificationNumber.Description = "Ikama Number";
        HcmPersonIdentificationNumber.Person      = dirPerson.RecId;
        HcmPersonIdentificationNumber.IdentificationType = 5637145329;
        HcmPersonIdentificationNumber.insert();
    }
    RCell = Cells.item(nRow,14);
    if(RCell.value().bStr() != "")
    {
        HcmPersonIdentificationNumber.IdentificationNumber = RCell.value().bStr();
        RCell = Cells.item(nRow,15);
        HcmPersonIdentificationNumber.ExpirationDate = RCell.value().date();
        HcmPersonIdentificationNumber.Description = "Passport Number";
        HcmPersonIdentificationNumber.Person      = dirPerson.RecId;
        HcmPersonIdentificationNumber.IdentificationType = 5637145328;
        HcmPersonIdentificationNumber.insert();
    }
    nRow++;
    RCell = Cells.Item(nRow, 2);
    DirPerson.clear();
    DirPartyTable.clear();
    dirpersonname.clear();
    hcmWorker.clear();
    HcmWorkerTitle.clear();
    HcmPersonIdentificationNumber.clear();
    HcmPersonPrivateDetails.clear();
    HcmEmployment.clear();
    }

    xlsApplication.quit ();
    xlsApplication.finalize ();
    info("Import completed");
    }
    catch( Exception::Error)
    {
    xlsApplication.quit ();
    xlsApplication.finalize ();
    ttsabort;
    info("Unable to process the excel import ");
    }
    }


Comments

  1. Thank You Pramod, could you share the excel/csv template

    ReplyDelete

Post a Comment

Popular posts from this blog

How to change client Language in ax 2012

Hello,  As you know that Dynmics Ax run on different languages. So I gonna tell you how to change client language country/region specific. this is nothing but a very simple step to do. Go To > File > Tools > Options > After that go to General tab and select the language which you want to use in your client. then apply and exit. In my case I was using Arabic language. Now restart your client and see the difference. You can do it from your database also . Go to Database server  . In my case it is sql server 2014. Select your database > tables > UserInfo > select the user > change the language code. Done Thanks.

Simple x++ query with enum value range

static void queryEnumValue(Args _args) {     Query                   query;     QueryRun                queryrun;     QueryBuildDataSource    qbds;     QueryBuildRange         qbr;     PurchTable              purchTable;     ;         query = new Query();         qbds = query.addDataSource(tableNum(PurchTable));     qbds.addSortField(fieldNum(PurchTable, PurchId), SortOrder::Ascending);     qbr     = qbds.addRange(fieldNum(PurchTable, PurchStatus));         qbr.value(enum2str(PurchStatus::Received));         queryrun = new QueryRun(query);         while(queryrun.next())     {         purchTable = query...

Unit Conversion by code in ax 2012 R3 , X++

static void unitConversion() {     SysExcelApplication xlsApplication;     SysExcelWorkBooks xlsWorkBookCollection;     SysExcelWorkBook xlsWorkBook;     SysExcelWorksheets xlsWorkSheetCollection;     SysExcelWorksheet xlsWorkSheet;     SysExcelRange xlsRange;     SysExcelCells Cells;     SysExcelCell RCell;     CommaIO inFile;     int nRow,i;     DialogField dialogPath;     Dialog dialog;     Filename filename;         UnitOfMeasureConversion             unitOfMeasureConversion;     UnitOfMeasure                       unitOfMeasure;     EcoResProduct                       ecoResProduct;     RecId               ...