web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

HcmWorker: Record already exists

(0) ShareShare
ReportReport
Posted on by 507

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++;
}

I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    300,917 Super User 2025 Season 2 on at

    Hi Huggins,

    You can use the debugger to find out on what exact point the exception is detected and what values are present or not.

    I do think it is because of the DirPersonRecId select statement. It will probably find an older record. Why aren't you directly using the RecId of the DirPerson table which you inserted?

    The statement to fill the RecId of the the DirPartyTable is also not something which would work correctly as an insert command will generate a new record ID.

  • Huggins Mafigu Profile Picture
    507 on at

    Currently that select statement is not working.

  • Verified answer
    André Arnaud de Calavon Profile Picture
    300,917 Super User 2025 Season 2 on at

    I do think you have to use the dirPerson.RecId for the HCMworker and not any other value.

  • Suggested answer
    Huggins Mafigu Profile Picture
    507 on at

    DirPerson dirPerson;

     DirPerson.Name          = firstname+" "+LastName;

     DirPerson.NameSequence  = 5637144576;

     DirPerson.LanguageId    = "en-us";

     dirperson.NameAlias     = SearchName;

     DirPerson.insert();

    hcmWorker.PersonnelNumber     = hcmContractSync.PersonnelNumber;

    hcmWorker.Person              = dirPerson.RecId;

    hcmWorker.insert();            

    Just like this??

  • Verified answer
    André Arnaud de Calavon Profile Picture
    300,917 Super User 2025 Season 2 on at

    Hi Huggins,

    Yes, you can try this. I don't have too much experience in creating workers via coding, but at least the dirPerson reference is correct here.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans