Home

Wednesday, May 4, 2016

Create vendor from code Ax 2012 X++

static void SPS(Args _args)
{
    #define.Name("First Middle Last")
    #define.FirstName("First ")
     #define.MiddleName("Middle ")
    #define.LastName("Last")
    #define.PostalAddressName("Postal address")
    #define.City("City Name")
    #define.County("India")
    #define.Country("India")
    #define.Street("This is street Address")
    #define.State("STY")
    #define.ZipCode("100092")
    #define.ElectronicAddressName("Electronic address")
    #define.Locator("first@microsoft.com")
    #define.LocatorExt("")
    #define.VendAccount("VendXPP001")
    #define.VendGroup("10")
    #define.Currency("INR")
    #define.CostCenter("010")
    #define.Department("025")

    DirPerson                   dirPerson;
    DirPersonName               dirPersonName;
    LogisticsLocation           logisticsLocation;
    LogisticsPostalAddress      logisticsPostalAddress;
    LogisticsElectronicAddress  logisticsElectronicAddress;
    VendTable                   vendTable;

    try
    {
        ttsbegin;

        //Person
        dirPerson.clear();
        dirPerson.initValue();
        dirPerson.Name = #Name;

        if (dirPerson.validateWrite())
        {
            dirPerson.insert();

            if (dirPerson)
            {
                //Person name
                dirPersonName.clear();
                dirPersonName.initValue();
                dirPersonName.FirstName = #FirstName;
                dirPersonName.MiddleName = #MiddleName;
                dirPersonName.LastName = #LastName;
                dirPersonName.Person = dirPerson.RecId;
                dirPersonName.ValidFrom = DateTimeUtil::utcNow();
                dirPersonName.ValidTo = DateTimeUtil::maxValue();

                if (dirPersonName.validateWrite())
                {
                    dirPersonName.insert();
                }
                else
                    throw error("Person name");

                //Location
                logisticsLocation = LogisticsLocation::create(#PostalAddressName, NoYes::Yes);

                //Party location
                DirParty::addLocation(dirPerson.RecId, logisticsLocation.RecId, true, true, false, [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId]);

                //Postal address
                logisticsPostalAddress.clear();
                logisticsPostalAddress.initValue();
                logisticsPostalAddress.City = #City;
                logisticsPostalAddress.County = #County;
                logisticsPostalAddress.CountryRegionId = #Country;
                logisticsPostalAddress.Street = #Street;
                logisticsPostalAddress.State = #State;
                logisticsPostalAddress.ZipCode = #ZipCode;
                logisticsPostalAddress.Address = LogisticsPostalAddress::formatAddress(
                #Street, #ZipCode, #City, #Country, #State, #County);
                logisticsPostalAddress.Location = logisticsLocation.RecId;
                logisticsPostalAddress.ValidFrom = DateTimeUtil::utcNow();
                logisticsPostalAddress.ValidTo = DateTimeUtil::maxValue();

                if (logisticsPostalAddress.validateWrite())
                {
                    logisticsPostalAddress.insert();
                }
                else
                    throw error("Postal address");

                //Location
                logisticsLocation = LogisticsLocation::create(#ElectronicAddressName, NoYes::No);

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

                //Electronic address
                logisticsElectronicAddress.clear();
                logisticsElectronicAddress.initValue();
                logisticsElectronicAddress.Location = logisticsLocation.RecId;
                logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Email;
                logisticsElectronicAddress.Locator = #Locator;
                logisticsElectronicAddress.LocatorExtension = #LocatorExt;


                if (logisticsElectronicAddress.validateWrite())
                {
                    logisticsElectronicAddress.insert();
                }
                else
                    throw error("Electronic address");

                //Vendor
                vendTable.clear();
                vendTable.initValue();
                //vendTable.Currency = "";

                vendTable.AccountNum = #VendAccount;
                //vendTable.AccountNum = NumberSeq::newGetNum(VendParameters::numRefVendAccount()).num();
                vendTable.Party = dirPerson.RecId;

                vendTable.VendGroup = #VendGroup;
                vendTable.initFromVendGroup(VendGroup::find(#VendGroup));

                vendTable.Currency = #Currency;
                vendTable.DefaultDimension = AxdDimensionUtil::getDimensionAttributeValueSetId(
                [2, "CostCenter", #CostCenter, "Department", #Department]);

                if (vendTable.validateWrite())
                {
                    vendTable.insert();
                }
                else
                    throw error("Vendor");
            }
        }
        else
            throw error("Person");

        ttscommit;
    }
    catch
    {
        error("Error!");
        return;
    }

    info("Done!");
}

No comments:

Post a Comment