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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Create worker through code

(0) ShareShare
ReportReport
Posted on by 552

Hi experts,

I don't want to import worker. I have name and other related information of a worker. I need to create a worker through X++.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Bashir Ahmad Profile Picture
    5,248 on at

    Try it:

    www.andesoft.net/create-employee-using-x-code

  • Verified answer
    Sohaib Cheema Profile Picture
    49,664 Super User 2026 Season 1 on at

    long back I wrote some code for that. Let me share that, you may modify that as per your needs

    Here it goes

     

     public static void CreateEmployee(FirstName _firstName,
                                      MiddleName _middleName,
                                      LastName _lastName,
                                      str _email,
                                      HcmPersonnelNumberId _employeeId,
                                      date _joiningDate,
                                      HcmTitleId _title,
    SelectableDataArea _ReleaseInCompany
    ) { DirPerson dirPerson; DirPersonName dirPersonName; HcmWorker newHcmWorker; HcmWorkerTitle hcmWorkerTitle; LogisticsLocation logisticsLocation; LogisticsLocation lLogisticsLocation; DirPartyContactInfoView dirPartyContactInfoView; DirParty dirParty; HcmEmploymentRecId newEmploymentRecId; ValidFromDateTime employmentStartDateTime; ValidToDateTime employmentEndDateTime; str employeeEmailAdress; HcmPersonnelNumberId employeeid; RecId compayRecId; ; dirPersonName.FirstName = _firstName; dirPersonName.MiddleName = _middleName; dirPersonName.LastName = _lastName; employeeEmailAdress = _email; employeeid = _employeeId; compayRecId = CompanyInfo::findDataArea(_ReleaseInCompany).RecId; employmentStartDateTime = DateTimeUtil::newDateTime(_joiningDate,24); employmentEndDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone()); if(!HcmWorker::findByPersonnelNumber(employeeid)) { newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName , employeeid , compayRecId , HcmEmploymentType::Employee , employmentStartDateTime , employmentEndDateTime)); dirParty = new DirParty(DirPerson::find(dirPersonName.Person)); if(employeeEmailAdress != '' && newHcmWorker.Person != 0) { logisticsLocation.clear(); logisticsLocation = LogisticsLocation::create('Email', NoYes::No); dirPartyContactInfoView.LocationName = 'Primay Email'; dirPartyContactInfoView.Locator = employeeEmailAdress; dirPartyContactInfoView.Type = LogisticsElectronicAddressMethodType::Email; dirPartyContactInfoView.Party = DirPerson::find(newHcmWorker.Person).RecId; dirPartyContactInfoView.IsPrimary = NoYes::Yes; dirParty.createOrUpdateContactInfo(dirPartyContactInfoView); } if (newHcmWorker.RecId != 0) { ttsBegin; hcmWorkerTitle.clear(); hcmWorkerTitle.Worker = newHcmWorker.RecId; hcmWorkerTitle.ValidFrom = DateTimeUtil::newDateTime(_joiningDate,0); hcmWorkerTitle.ValidTo = employmentEndDateTime; hcmWorkerTitle.Title = HcmTitle::findByTitle(_title).RecId; hcmWorkerTitle.insert(); ttsCommit; } /*if (newHcmWorker.RecId != 0) { info(newHcmWorker.PersonnelNumber+" Employee Created"); }*/ } else { info(HcmWorker::findByPersonnelNumber(employeeid).PersonnelNumber+" already exists"); } }


     

  • Mallika Roy Profile Picture
    552 on at

    Thanks Sohaib.

  • xain Profile Picture
    504 on at

    HI Sohaib,

    i am also looking for same thing but i also want to include marital status and numberofdependent fields but they are in hcmPersonDetails table. how can i incorporate them. kindly guide me. 

    Thanks

  • Sohaib Cheema Profile Picture
    49,664 Super User 2026 Season 1 on at

    Hi xain,

    Divide your task into two parts

    1) create worker

    2) import hcmPersonDeatils

    if I will be in your shoes, I will love to rely on DIXF. Writing code is just  a matter of wasting time, if something is available as data entity. Have a look at entities, if you can use one or you may create a custom entity.

  • xain Profile Picture
    504 on at

    HI Sohaib,

    have a look at things that i have tried but couldn't get it figure out. 

    addingfieldserror.rar

    P.S. i also had posted my question on this thread. 

    https://community.dynamics.com/ax/f/33/p/251405/702934#702934 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 34

#2
Michel ROY Profile Picture

Michel ROY 14

#3
Jagadabi Profile Picture

Jagadabi 6

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans