I'm trying to add a record into HcmWorker but I'm getting a 'Cannot create a record in Worker (HcmWorker). The record already exists.'. I then created a function for updating and flushing cache trying to do away with the error but to no avail. The following is my code which results in the 'Cannot create a record in Worker (HcmWorker). The record already exists.' error. Anyone who can help am stuck?? What do I need to do or what exactly is wrong with my code??
public void clicked()
{
super();
int recordCount =0;
HcmContract hcmContractLines,local,hcmContractSync;
HcmWorker hcmWorker;
;
super();
for (hcmContractLines = hcmContract_ds.getFirst(true) ? hcmContract_ds.getFirst(true) : hcmContractLines; hcmContractLines; hcmContractLines = hcmContract_ds.getNext())
{
ttsbegin;
select forupdate local where local.PersonnelNumber == hcmContract.PersonnelNumber;
//Synchronize confirmed records
//select hcmContractSync where hcmContractSync.RecordStatus == 1 && hcmContractSync.Synchronized == NoYes::No;
Name firstName, LastName;
LegalEntity LegalEntity;
DirPerson dirPersonRecId;
HcmPersonnelNumberId PersonnelNo;
HcmEmploymentType WorkerType;
DirPerson dirPerson;
DirPartyTable DirPartyTable;
HcmEmployment HcmEmployment;
DirPersonName DirPersonName;
HcmPersonPrivateDetails HcmPersonPrivateDetails;
HcmPersonIdentificationNumber HcmPersonIdentificationNumber;
HcmWorkerTitle hcmWorkertitle;
str WorkLocation;
// Variables for Number Sequence in case you want to generate number sequence on synchronization
// Same logic can also be used for importing employees into AX 2012 or D365Finance and Operations
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;
HcmPersonGender hcmPersonGender;
HcmPositionDuration HcmPositionDuration;
try
{
FirstName = hcmContractSync.FirstName;
LastName = hcmContractSync.LastName;
SearchName = firstname+" "+LastName;
birthdate = hcmContractSync.DateOfBirth;
CountryCode = 'ZW';
WorkLocation = hcmContractSync.BasedAt; //'Building B - 5215';
DirPerson.Name = firstname+" "+LastName;
DirPerson.NameSequence = 5637144576;
DirPerson.LanguageId = "en-us";
dirperson.NameAlias = SearchName;
DirPerson.Gender = str2Enum(hcmPersonGender, enum2Str(hcmContractSync.Gender));
DirPerson.insert();
//select dirPersonRecId where dirPersonRecId.RecId == Global::maxRecId();
select maxof(RecId) from dirPersonRecId;
info(strFmt('%1', dirPersonRecId.RecId));
DirPartyTable.RecId = dirPersonRecId.RecId;
DirPartyTable.insert();
dirpersonname.FirstName = firstname;
dirpersonname.LastName = LastName;
DirPersonName.Person = dirPersonRecId.RecId; //getLastDirPersonRecId(); //dirPerson.RecId;
DirPersonName.ValidFrom = DirUtility::getCurrentDateTime(); //DateTimeUtil::newDateTime(local.AssignmentStartDate,0);
DirPersonName.ValidTo = DateTimeUtil::maxValue();//DateTimeUtil::newDateTime(local.AssignmentEndDate, 0);
dirpersonname.insert();
hcmWorker.PersonnelNumber = hcmContractSync.PersonnelNumber;
hcmWorker.Person = dirPersonRecId.RecId; ///dirPerson.RecId;
hcmWorker.insert();
HcmWorkerTitle.OfficeLocation = WorkLocation;
HcmWorkerTitle.ValidFrom = DateTimeUtil::newDateTime(today(),0);
HcmWorkerTitle.ValidTo = DateTimeUtil::maxValue();
HcmWorkerTitle.OriginalHireDateTime = DateTimeUtil::newDateTime(today(),0);
HcmWorkerTitle.Worker = hcmWorker.RecId;
HcmWorkerTitle.insert();
// HcmEmployement table data
HcmEmployment.EmploymentType = HcmEmploymentType::Employee;
HcmEmployment.LegalEntity = CompanyInfo::find().RecId;
HcmEmployment.ValidFrom = DateTimeUtil::newDateTime(local.AssignmentStartDate,0);
HcmEmployment.ValidTo = DateTimeUtil::newDateTime(local.AssignmentEndDate, 0);
HcmEmployment.Worker = hcmWorker.RecId;
HcmEmployment.insert();
// Worker private details (HCMPersonPrivateDetails)
HcmPersonPrivateDetails.BirthDate = birthdate;
HcmPersonPrivateDetails.CitizenshipCountryRegion = CountryCode;
HcmPersonPrivateDetails.Gender = str2Enum(hcmPersonGender, enum2Str(hcmContractSync.Gender));
HcmPersonPrivateDetails.Person = dirPersonRecId.RecId; //dirPerson.RecId;
HcmPersonPrivateDetails.insert();
// Worker identification numbers
HcmPersonIdentificationNumber.IdentificationNumber = hcmContractSync.IdNumber;// RCell.value().bStr();
HcmPersonIdentificationNumber.ExpirationDate = DateTimeUtil::maxValue();// RCell.value().date();
HcmPersonIdentificationNumber.Description = "National Id Number";
HcmPersonIdentificationNumber.Person = dirPersonRecId.RecId; //dirPerson.RecId;
HcmPersonIdentificationNumber.IdentificationType = 22565420923; //5637145329;
HcmPersonIdentificationNumber.insert();
DirPerson.clear();
DirPartyTable.clear();
dirpersonname.clear();
hcmWorker.clear();
HcmWorkerTitle.clear();
HcmPersonIdentificationNumber.clear();
HcmPersonPrivateDetails.clear();
HcmEmployment.clear();
local.RecordStatus = 1;
local.update();
ttscommit;
info("Confirm completed");
}
catch( Exception::Error)
{
ttsabort;
info("Unable to process the syncronization of records");
}
recordCount++;
}