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
- DirPartyTable
- HcmWorker
- HcmEmploymentDetail
- HcmEmployment
- DirPersonName
- 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(); }