Friday 6 May 2016

How to create Worker to Vendor using X++ Code in Ax 2012

static void Raj_WorkertoVendorCreate(Args _args)
{
    VendTable                           vendTable;
    HcmWorker                           hcmWorker;
    LogisticsLocation                   logisticsLocation;
    HcmEmployment                       hcmEmployment;
    ;

    try
    {
        while select hcmWorker
            join hcmEmployment
                where hcmEmployment.Worker == hcmWorker.RecId
                    && hcmEmployment.EmploymentType == HcmEmploymentType::Employee
        {
            ttsbegin;
            changeCompany(CompanyInfo::findRecId(hcmEmployment.LegalEntity).DataArea)
            {

                if(VendTable::find(hcmWorker.PersonnelNumber))
                {
                    //updates vendor(If we edit details i.e name)
                    vendTable = VendTable::find(hcmWorker.PersonnelNumber,true);
                    if(DirPartyTable::findRec(vendTable.Party).PrimaryAddressLocation)
                    {
                        logisticsLocation = LogisticsLocation::find(DirPartyTable::findRec(vendTable.Party).PrimaryAddressLocation);
                        if(logisticsLocation)
                        {
                            //Party location
                            DirParty::addLocation(vendTable.Party, logisticsLocation.RecId, true, true, false, [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId]);
                        }
                    }
                    if(vendTable.DefaultDimension != hcmEmployment.DefaultDimension)
                        vendTable.DefaultDimension = hcmEmployment.DefaultDimension;

                    if(vendTable.validateWrite())
                    {
                        vendTable.update();
                       // info(strFmt("Vendor - %1 is Updated", vendTable.AccountNum));
                    }
                    else
                        throw error("Please enter valid details");
                }
                else
                {
                    //Location
                    if(DirPartyTable::findRec(hcmWorker.person).PrimaryAddressLocation)
                    {
                        logisticsLocation = LogisticsLocation::find(DirPartyTable::findRec(hcmWorker.person).PrimaryAddressLocation);
                        if(logisticsLocation)
                        {
                            //Party location
                            DirParty::addLocation(hcmWorker.person, logisticsLocation.RecId, true, true, false, [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId]);
                        }
                    }

                    //Party location
                    DirParty::addLocation(hcmWorker.person, logisticsLocation.RecId, false, true, false);

                    //creates Vendor
                    vendTable.clear();
                    vendTable.initValue();

                    vendTable.AccountNum = hcmWorker.personnelNumber;
                    vendTable.Party = hcmWorker.person;

                    vendTable.VendGroup = "EMP";
                    vendTable.initFromVendGroup(VendGroup::find("EMP"));

                    vendTable.Currency = CompanyInfo::standardCurrency();
                    vendTable.DefaultDimension =hcmEmployment.DefaultDimension;


                    if (vendTable.validateWrite())
                    {
                        vendTable.insert();
                        //info(strFmt("Vendor - %1 is created", vendTable.AccountNum));
                    }
                    else
                        throw error("Please enter valid details");
                }
            }
            ttscommit;
        }
    }
    catch
    {
        throw error("Error while creating/updating vendor.");
    }

}

No comments:

Post a Comment