Partager via


AX 2012 - Create new Name & Address through code

static void createNameAndAddress(Args _args)
{
    DirPartyPostalAddressView   addressView;
    Name                        name;
    DirPartyRecId               partyRecId;
    DirParty                    dirParty;
    ;
    //Create new Name
    partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;
   
    addressView.CountryRegionId = "USA";
    addressView.State = "OH";
    addressView.ZipCode = "00210";
    addressView.Street  = "Park Ave";
    addressView.Party = partyRecId;
    
    //Handel errors for addresses
    if (!LogisticsAddressCountryRegion::exist(addressView.CountryRegionId))
        throw error(strFmt("@SYS9347",addressView.CountryRegionId));
    if (addressView.County && !LogisticsAddressCounty::exist(addressView.CountryRegionId, addressView.State, addressView.County))
        throw error(strFmt("@SYS72719",addressView.County));
    if (addressView.State && !LogisticsAddressState::exist(addressView.CountryRegionId, addressView.State))
        throw error(strFmt("@SYS72786",addressView.State));
    if (!LogisticsAddressZipCode::exist(addressView.ZipCode))
        throw error(strFmt("@SYS24626",addressView.ZipCode));

    addressView.Party  = partyRecId;
    if( addressView.Street || addressView.ZipCode || addressView.City || addressView.State || addressView.CountryRegionId)
    {
        DirParty = DirParty::constructFromPartyRecId(addressView.Party );
        DirParty.createOrUpdatePostalAddress(addressView);
    }

}