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 :
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,438 User Group Leader 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,438 User Group Leader 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

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans