Parag Chapre

How to create a worker in x++

How to create worker in x++
Spread the love

In today’s article, we will see how to create a worker using X++.

Step 1: Create a class and define the global variable.

class TestHireNewWorker
ValidFromDateTime DefaultValidFromDateTime = HcmDateTimeUtil::startOfCurrentDay();
FirstName firstName = 'Julia';
LastName lastName = 'Reeves';
HcmTitleId hcmTitleId = 'Mrs';
HcmPositionRecId positionRecId;
HcmWorkerRecId workerRecId;
CompanyInfoRecId legalEntityRecId = CompanyInfo::current();
DirPersonName personName;
utcdatetime startDate = HcmDateTimeUtil::startOfCurrentDay();
utcdatetime endDate = DateTimeUtil::maxValue();

Step 2: Create new method to insert Title in the master table.

 public  HcmTitle createTitle(HcmTitleId _hcmTitleId)
    {
        HcmTitle hcmTitle = HcmTitle::findByTitle(_hcmTitleId);
        if(!hcmTitle)
        {
			hcmTitle.initValue();
			hcmTitle.TitleId = _hcmTitleId;
			hcmTitle.doInsert();
        }
        return hcmTitle;
    }

Step 3: Create a new method to enter first name, middle name and last name in the Dir party table.

private DirPersonName createPersonName(FirstName _firstName, MiddleName _middleName, LastName _lastName)
    {
        DirPerson dirPerson;
        dirPerson.initValue();
        DirPersonName dirPersonName;
        dirPersonName.FirstName = _firstName;
        dirPersonName.MiddleName = _middleName;
        dirPersonName.LastName = _lastName;
        dirPersonName.ValidFrom = DefaultValidFromDateTime;
        dirPersonName.ValidTo = DateTimeUtil::maxValue();
        dirPerson.updateName(dirPersonName);
        dirPerson.insert();
        dirPersonName.Person = dirPerson.RecId;
        dirPersonName.doInsert();
        return dirPersonName;
    }

Step 4: Create a new method to insert data for HcmWorker titles.

public HcmWorkerTitle createWorkerTitle(HcmWorker _worker, HcmTitle _title)
		{
			HcmWorkerTitle hcmWorkerTitle;
			hcmWorkerTitle.initValue();
			hcmWorkerTitle.Title  = _title.RecId;
			hcmWorkerTitle.Worker = _worker.RecId;
			hcmWorkerTitle.ValidFrom = DateTimeUtil::minValue();
			hcmWorkerTitle.ValidTo   = DateTimeUtil::maxValue();
			hcmWorkerTitle.insert();
			return hcmWorkerTitle;
		}

Step 5: Create a run method and call above method.

Note: You can create position and assign the record id and dates. I am not using position details.

public void run()
	{
        HcmPersonnelNumberId personnelNumber = HcmWorker::getNewPersonnelNumber();
        personName = this.createPersonName(firstName, '', lastName);
        HcmHireNewWorkerContract hireNewWorkerContract = HcmHireNewWorkerContract::construct();
        hireNewWorkerContract.parmDirPersonName(personName);
        hireNewWorkerContract.parmPersonnelNumber(personnelNumber);
      //  hireNewWorkerContract.parmPositionRecId(positionRecId);  Assign position to worker
        //  hireNewWorkerContract.parmPositionAssignmentStartDate(startDate); Assign position assignment start date
        //  hireNewWorkerContract.parmPositionAssignmentEndDate(endDate); Assign position assignment end date
        hireNewWorkerContract.parmEmploymentValidFrom(startDate); //Assign employment start date
        hireNewWorkerContract.parmEmploymentValidTo(endDate); //Assign employment end date
        hireNewWorkerContract.parmLegalEntityRecId(legalEntityRecId); //Assign current legal entity
        hireNewWorkerContract.parmEmploymentType(HcmEmploymentType::Employee);
        workerRecId = HcmWorkerTransition::newHireHcmWorkerV2(hireNewWorkerContract);
        HcmWorker hcmWorker = HcmWorker::find(workerRecId);
        HcmTitle hcmTitle = this.createTitle(hcmTitleId);
        this.createWorkerTitle(hcmWorker,hcmTitle);
	}

Step 6: Create a main method, instance of class and call the run method from main. method.

public static void main(Args _args)
    {
        TestHireNewWorker TestHireNewWorker = new TestHireNewWorker();
        TestHireNewWorker.run();
    }

You can download above code using this link.

If you like this article, feel free to share it with others who might find it helpful! If you have any questions, feel free to reach out to me.

1 Comment

  1. […] DirPersonName personName; utcdatetime startDate = HcmDateTimeUtil::startOfCurrentDay();…… Continue Reading → Read Complete Post and Comments SBX – Two Col […]

Leave a Reply

Your email address will not be published. Required fields are marked *