Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

Contractual Worker's Excel Uploader not showing in grid (FK Replacement failed)

(0) ShareShare
ReportReport
Posted on by 131

I am creating an excel uploader for uploading contractual workers in AX 2012 and it is inserting data into tables but it is not showing data in grid and on debugger mode when I mouse over on hcmworker.person then it shows FK Replacement failed but also inserting that recid of dirpartytable into hcmworker.person.

I have also compared the uploaded data with ax 2012 default inserted data  so what is the reason of it and what's the solution. I am inserting data into following table

  1. DirPartyTable
  2. HcmWorker 
  3. HcmEmploymentDetail
  4. HcmEmployment 
  5. DirPersonName
  6. HcmWorkerTitle 

I have created a class for this uploader and created two method one (ExcelUploaderForContractualWorker) for uploading data and second (getrecid) for getting HCMTITLE recid for inserting in HCMWORKERTITLE. Code are given below:-

class ExcelUploaderForContractualWorker
{
     HcmTitle                    _hcmtitle;
}

private void ExcelUploaderForContractualWorker()
{
    RecId                       _recid;
    ValidToDateTime             _validto;
    DirPartyTable               _dirtytable;
    HcmWorker                   _hcmworker;
    HcmEmploymentDetail         _hcmemploymentdetail;
    HcmEmployment               _hcmemployment;
    DirPersonName               _dirpersonname;
    HcmWorkerTitle              _hcmworkertitle;
   // HcmTitle                    _hcmtitle;
    //int64 _recid;
    //str s;
    String30 s,x;

    Dialog dialog;
    Filename filename;
    DialogField dialogFilename;
    //To filter the files while selecting
    container conFilter = ["Microsoft Excel 97-2003 Worksheet (.xls)" ,"*.xlsx"];

    //Excel
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;

    int row = 1;
    int line= 1;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    dialog = new dialog();
    dialog.caption("select a file");
    dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
    //To filter the files while selecting
    dialog.filenameLookupFilter(conFilter);
    dialog.run();

    if(dialog.closedOk())
    {
        filename = dialogFileName.value();
    }

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();

        do
    {
        row  ;


        ttsbegin;

       // _validto = cells.item(row,6).value().date;

            _dirtytable.Name = cells.item(row,2).value().bStr();
            _dirtytable.LanguageId = strAlpha("en-us");

            _dirtytable.insert();
            
        _dirtytable = DirPartyTable::findRec(_dirtytable.RecId);
         if (_dirtytable.RecId)
        {
            _dirpersonname.Person =_dirtytable.RecId;
            _hcmworker.Person = _dirtytable.RecId;
        }
       

            
            _validto=str2datetime("31-12-2154 23:59:59.000",123);
           
            _dirpersonname.FirstName  = cells.item(row,2).value().bStr();
            _dirpersonname.MiddleName = cells.item(row,3).value().bStr();
            _dirpersonname.LastName = cells.item(row,4).value().bStr();
            _dirpersonname.ValidFrom=str2datetime(cells.item(row,6).value().bStr(),123);
            _dirpersonname.ValidTo=_validto;
            _dirpersonname.insert();

            
           
        
            //_hcmworker.Person = _recid;
            _hcmworker.PersonnelNumber = cells.item(row,1).value().bStr();
            _hcmworker.IdentificationNumber = cells.item(row,7).value().bStr();
            _hcmworker.insert();//0
          //  ReSearch();
        
             _hcmworker = HcmWorker::findByPersonnelNumber(cells.item(row,1).value().bStr());
        
          if (_hcmworker.RecId)
            {
               _hcmemploymentdetail.Employment = _hcmworker.RecId;
            }

            
            _validto=str2datetime(cells.item(row,6).value().bStr(),123);
            _hcmemploymentdetail.ValidFrom = str2datetime(cells.item(row,6).value().bStr(),123); //cells.item(row,1).value().date();
            _hcmemploymentdetail.ValidTo = _validto;
            _hcmemploymentdetail.insert();
            
             _hcmworker = HcmWorker::findByPersonnelNumber(cells.item(row,1).value().bStr());
        if (_hcmworker.RecId)
            {
                _hcmemployment.Worker = _hcmworker.RecId;
            }
        
            _hcmemployment.LegalEntity =5637145326;
            _hcmemployment.ValidFrom=str2datetime(cells.item(row,6).value().bStr(),123);
            _hcmemployment.ValidTo = _validto;
            _hcmemployment.EmploymentType = 2;
            _hcmemployment.insert();

          _hcmworkertitle.Title = str2int64(this.getrecid(cells.item(row,5).value().bstr()));
          // _hcmworkertitle.insert();



        ttscommit;
        // type = cells.item(row   1, 1).value().variantType();

    }

    while(type != COMVariantType::VT_EMPTY);

    workbooks.close();
    application.quit();

    info("Operation/Processing Completed");

}

private HcmTitleId getrecid(HcmTitleId _Title)
{
    str RecId;
     select firstonly RecId from _hcmTitle
                where _hcmTitle.TitleId == _Title;

    return RecId;

}

public static void main(Args _args)
{
    ExcelUploaderForContractualWorker obj = new ExcelUploaderForContractualWorker();
    obj.ExcelUploaderForContractualWorker();
}

worker


  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,784 Super User 2024 Season 2 on at
    RE: Contractual Worker's Excel Uploader not showing in grid (FK Replacement failed)

    Hi Syed,

    When you say you used the debugger, did you debug your import or the form? Please debug and stop on each insert in your coding to check if the data is correct and after the insert if you have a record ID.

    If your coding is working correctly and the data is not showing, then it might be the case that a workers start date is in the future or the end date in the past. In that case, the worker is visible on other list pages, like terminated workers.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,784 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,476 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans