Thursday, August 30, 2018

Create new HCMWorker (Worker)

Example of an x++ method for creating new Worker in AX2012 based on parameters as first name, last name, start date and we need preconfigured default position for hiring a new worker. This piece of code also updates existing worker.

protected void createWorker(){
    DirPersonName           dirPersonName;
    DirPerson               dirPerson;
    HcmWorker               newHcmWorker;
    CompanyInfo             companyInfo;
    HcmPosition             hcmPosition;
    HcmPositionDuration     hcmPositionDuration;
    HcmPositionDetail       hcmPositionDetail,
                            fromHcmPositionDetail;
    FirstName               firstName;
    LastName                lastName;
    ValidFromDateTime       startDate;
    ValidToDateTime         endDate;
    HcmPersonnelNumberId    workerId;
 
    workerId  = 'Worker-01';
    firstName = 'Worker First name';
    firstName = 'Worker Last name';
    startDate = DateTimeUtil::utcNow();
    endDate   = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
 
    ttsBegin;
    dirPersonName.FirstName = firstName;
    dirPersonName.LastName = lastName;
    companyInfo = CompanyInfo::find();

    newHcmWorker = HcmWorker::findByPersonnelNumber(workerId, true);

    //Hire new Worker if Worker not found
    if(!newHcmWorker)
    {
        fromHcmPositionDetail = HcmPositionDetail::findByPosition(11111111);// default position details to creating a new worker
        // Create a position with position details and duration
        hcmPosition.clear();
        hcmPosition.initValue();
        hcmPosition.PositionId = NumberSeq::newGetNum(NumberSeqReference::findReference(extendedTypeNum(HcmPositionId)), true).num();
        hcmPosition.insert();

        buf2Buf(fromHcmPositionDetail, hcmPositionDetail);
        hcmPositionDetail.Position  = hcmPosition.RecId;
        hcmPositionDetail.ValidFrom = startDate;
        hcmPositionDetail.ValidTo   = DateTimeUtil::maxValue();
        hcmPositionDetail.insert();

        hcmPositionDuration.initValue();
        hcmPositionDuration.Position    = hcmPosition.RecId;
        hcmPositionDuration.ValidFrom   = startDate;
        hcmPositionDuration.ValidTo     = DateTimeUtil::maxValue();
        hcmPositionDuration.insert();

        newHcmWorker = HcmWorker::find(HcmWorkerTransition::newHireHcmWorker(   dirPersonName,
                                                                                workerId,
                                                                                hcmPosition.RecId,
                                                                                startDate,
                                                                                endDate,
                                                                                startDate,
                                                                                endDate,
                                                                                companyInfo.RecId,
                                                                                HcmEmploymentType::Employee));
    }
    // Updating an existing worker
    else
    {
        // Updating an existing worker DirPersonName
        if (newHcmWorker)
        {
            dirPersonName = DirPersonName::find(newHcmWorker.Person);

            if(dirPersonName.FirstName != firstName || dirPersonName.LastName != lastName)
            {
                dirPersonName.clear();
                dirPersonName.FirstName = firstName;
                dirPersonName.LastName = lastName;

                dirPerson.initValue();
                dirPerson.updateName(dirPersonName);
                if (dirPerson.validateWrite())
                {
                    dirPerson.insert();

                    dirPersonName.Person = dirPerson.RecId;
                    dirPersonName.ValidFrom = DateTimeUtil::minValue();
                    dirPersonName.ValidTo = DateTimeUtil::maxValue();

                    if (dirPersonName.validateWrite())
                    {
                        dirPersonName.insert();
                    }
                }

                newHcmWorker.Person = dirPerson.RecId;

                if (newHcmWorker.validateWrite())
                {
                    newHcmWorker.update();
                }
            }

            //Update worker assignment
            HcmWorkerTransition::newUpdateHcmEmployment(
                HcmEmployment::findByWorkerLegalEntity(newHcmWorker.RecId, companyInfo.RecId),
                    startDate,
                    endDate);
        }
    }
    ttsCommit;
}

No comments:

Post a Comment