Thursday 26 January 2017

Set Delivery Address on Sales order through code X++ AX 2012

In this post we will learn how to set delivery address / over rider the default customer delivery address on a Sales order.

This method will return the  LogisticsPostalAddress recId, which we will assign to parm method of AXsalesTable class..
axsalesTable.parmDeliveryPostalAddress(this.setDeliveryAddress(_tmpSalesheaders));
private RecId setDeliveryAddress(TmpSalesTable           _tmpSalesheaders)
{
    DirPartyPostalAddressView   addressView, ret;
    DirPartyRecId                        partyRecId;
    DirParty                                 dirParty;
    LogisticsPostalAddress          postalAddress;
    ;
    //Create new Name
    partyRecId = DirPartyTable::createNew(DirPartyType::None, _tmpSalesheaders.DeliveryName).RecId;
   
    addressView.CountryRegionId = _tmpSalesheaders.DeliveryCountryRegionId;//"AUS";
    addressView.State = _tmpSalesheaders.DeliveryState;//"VIC";
    addressView.ZipCode = _tmpSalesheaders.DeliveryZipCode;//"3000";
    addressView.Street  = _tmpSalesheaders.DeliveryStreet;//"238 Flinders St";
    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 );
        ret = DirParty.createOrUpdatePostalAddress(addressView);
        postalAddress = LogisticsPostalAddress::findByLocation(ret.Location);
    }
    return postalAddress.RecId;
}

No comments:

Update NuGet package to new MS D365FO version

1. Import the NuGet package files from LCS for that particular version please take the PU version files only. a. Goto LCS-->Asset Libra...