Blog

Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Wednesday, May 14, 2014

Import new Worker through code in ax 2012

Firstly create an excel file that contains some fields like name, searchname, personnal no and worker type.
then create a record and run this job
static void importRecord(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 Name;
    LegalEntity LegalEntity;
    HcmPersonnelNumberId PersonnelNo;
    HcmEmploymentType WorkerType;
    HcmWorker hcmWorker;
    DirPerson dirPerson;
    DirPartyTable DirPartyTable;
    HcmEmployment HcmEmployment;
    DirPersonName DirPersonName;
    HcmPersonPrivateDetails HcmPersonPrivateDetails;
    importworker importworker;
    NameAlias SearchName;
    LogisticsElectronicAddressLocator Telephone;
    LogisticsElectronicAddress LogisticsElectronicAddress;
    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, 1);
    while (RCell.value().bstr() != "")
    {
    Name = RCell.value().bStr();
    RCell = Cells.item(nRow,2);
    SearchName = RCell.value().bStr();
    RCell = Cells.item(nRow,3);
    PersonnelNo = RCell.value().bStr();
    RCell = Cells.item(nRow,4);
    WorkerType = str2enum(WorkerType,RCell.value().bStr());
    RCell = Cells.item(nRow,5);
    Telephone = RCell.value().bStr();
    DirPerson.Name=Name;
    hcmWorker.PersonnelNumber = PersonnelNo;
   // DirPartyTable.name = Name;
    HcmEmployment.EmploymentType = WorkerType;
    dirpersonname.FirstName =  Name;
    dirperson.NameAlias = SearchName;
    LogisticsElectronicAddress.Locator =  Telephone;
    //hcmWorker.Person=dirPerson.RecId;
    //exportImportRecords.income = income;
    //importworker.insert();
    //dirPerson.RecId=DirPartyTable.RecId;
    DirPerson.insert();
   // DirPartyTable.RecId=dirPerson.RecId;
    //DirPartyTable.insert();
    DirPersonName.Person=dirPerson.RecId;
    dirpersonname.insert();
    LogisticsElectronicAddress.insert();
    hcmWorker.Person=DirAddressBookPartyAllView.Party;
    hcmWorker.Person=dirPerson.RecId;
   // HcmEmployment.insert();
    hcmWorker.insert();
     HcmEmployment.LegalEntity = 5637148640;
     HcmEmployment.ValidFrom=2007-05-05T23:22:21;
    HcmEmployment.ValidTo = 2015-05-05T23:22:21;
    HcmEmployment.Worker=hcmWorker.RecId;
    HcmEmployment.insert();
    //hcmWorker.update();
    nRow++;
    RCell = Cells.Item(nRow, 1);
    }
    xlsApplication.quit ();
    xlsApplication.finalize ();
    info("Import completed");
    }
    catch( Exception::Error)
    {
    xlsApplication.quit ();
    xlsApplication.finalize ();
    ttsabort;
    info("Unable to process the excel import ");
    }
    }

No comments:

Post a Comment