Pages

Wednesday, April 9, 2014

A bit of code for creating a worker in AX 2012

private HcmPersonnelNumberId nextPersonnelNumberId()
{
    HcmPersonnelNumberId    hcmPersonnelNumberId;
    NumberSequenceReference numberSeqReferencePersonnelNum;
    NumberSequenceTable     numberSeqTablePersonnelNum;
    NumberSeq               numberSeqPersonnelNum;

    // Number Sequence
    numberSeqReferencePersonnelNum  = NumberSeqReference::findReference(extendedTypeNum(HcmPersonnelNumberId));
    numberSeqTablePersonnelNum      = NumberSequenceTable::find(numberSeqReferencePersonnelNum.NumberSequenceId);

    if (numberSeqTablePersonnelNum)
    {
        // generate worker personnel number if number sequence code is created.
        numberSeqPersonnelNum = NumberSeq::newGetNumFromId(numberSeqTablePersonnelNum.RecId, true, true);

        if(numberSeqPersonnelNum)
        {
            hcmPersonnelNumberId = numberSeqPersonnelNum.num();
        }
    }
    return hcmPersonnelNumberId;
}

private void createHCMWorker()
{
    AxDirPerson             person;
    AxDirPersonName         personName;
    HcmWorker               worker;
    HcmPersonnelNumberId    hcmPersonnelNumberId;

    hcmPersonnelNumberId = this.nextPersonnelNumberId();

    if (!hcmPersonnelNumberId)
        return;

    // Create a person
    person = AxDirPerson::construct();
    person.parmName("Santa Claus");
    person.save();

    personName = AxDirPersonName::construct();
    personName.parmPerson(person.dirPerson().RecId);
    personName.parmFirstName(person.dirPerson().Name);
    personName.parmValidTo(DateTimeUtil::maxValue());
    personName.save();

    // Create the worker
    HcmWorkerTransition::newCreateHcmWorker(personName.dirPersonName(), hcmPersonnelNumberId);
}

No comments: