Monday 13 July 2020

Create address at run time in Dynamics 365 FinOps

DAX Folks,

Here is a quick code to create the delivery address at runtime. You can use this while creating new Purchase requisition, Purchase order, Sales order or a person.

Today, we are going to create One-Time address and Delivery address for a particular process. Lets choose a process of sales order  and the requirement is- to create one time address or delivery address based on a flag.

Have a look on below code,
  • Create One-time address at run time in D365 FO
So we are going to use LogisticsPostalAddressEntity to get the right address with a new one or an existing one. This code should also handle if there is any update in any existing record by updating effective date stamp.

Here, xxxSalesTableStaging is the staging table which should have all the necessary address component's data like Country region, Zip code, State, City, Street and etc... 

private LogisticsPostalAddressRecId createOneTimePostalAddress(xxxSalesTableStaging _stagingTable)
    {
        LogisticsAddressing                             addressing;
        LogisticsPostalAddress                          logisticsPostalAddress;
        LogisticsPostalAddressView                      postalAddressView, newPostalAddressView;
        LogisticsPostalAddressEntity                    postalAddressEntity;
        DirPartyPostalAddressView                       newPartyAddressView;
        LogisticsPostalAddressRecId                     postalAddressRecId;
        LogisticsPostalAddressStringBuilderParameters   addressStringBuilderParameters = new LogisticsPostalAddressStringBuilderParameters();
                 
        addressStringBuilderParameters.parmCountryRegionId(_stagingTable.CountryRegionId);
        addressStringBuilderParameters.parmZipCodeId(_stagingTable.ZipCode);
        addressStringBuilderParameters.parmStateId(_stagingTable.State);
        addressStringBuilderParameters.parmCityName(_stagingTable.City);
        addressStringBuilderParameters.parmStreet(_stagingTable.Street);
             
        addressing = LogisticsPostalAddressStringBuilder::buildAddressStringFromParameters(addressStringBuilderParameters);

        select firstonly postalAddressView
            where postalAddressView.Address  == addressing;

        if (postalAddressView)
        {
            postalAddressRecId = postalAddressView.PostalAddress;
        }
        else
        {
            logisticsPostalAddress.CountryRegionId  = _stagingTable.CountryRegionId;
            logisticsPostalAddress.ZipCode          = _stagingTable.ZipCode;
            logisticsPostalAddress.State            = _stagingTable.State;
            logisticsPostalAddress.City             = _stagingTable.City;
            logisticsPostalAddress.Street           = _stagingTable.Street;

            newPartyAddressView.initFromPostalAddress(logisticsPostalAddress);
            newPartyAddressView.LocationName = _stagingTable.DeliveryName ? _stagingTable.DeliveryName : custTable.name();

            postalAddressEntity = LogisticsPostalAddressEntity::construct();

            newPostalAddressView.initFromPartyPostalAddressView(newPartyAddressView);
            logisticsPostalAddress = postalAddressEntity.createPostalAddress(newPostalAddressView);

            postalAddressRecId = logisticsPostalAddress.RecId;
        }

        return postalAddressRecId;
    }

  • Create Delivery address at run time in D365 FO

 So we are going to use DirParty and LogisticsLocationRole to get the party and role type. LogisticsPostalAddressEntity will also be used to get the right address with a new one or an existing one. This code should also handle if there is any update in any existing record by updating effective date stamp.

Here, i am getting custTable from global variable (you can take any customer account for your testing) and xxxSalesTableStaging is the staging table which should have all the necessary address component's data like Country region, Zip code, State, City, Street and etc...

private LogisticsPostalAddressRecId createDeliveryPostalAddress(HSSalesTableStaging _stagingTable)
    {
        DirParty                                        dirParty;
        container                                       roleIds;
        LogisticsAddressing                             addressing;
        LogisticsPostalAddress                          postalAddress;
        DirPartyPostalAddressView                       partyAddressView, newPartyAddressView;
        LogisticsPostalAddressRecId                     postalAddressRecId;
        LogisticsPostalAddressStringBuilderParameters   addressStringBuilderParameters = new LogisticsPostalAddressStringBuilderParameters();

        addressStringBuilderParameters.parmCountryRegionId(_stagingTable.CountryRegionId);
        addressStringBuilderParameters.parmZipCodeId(_stagingTable.ZipCode);
        addressStringBuilderParameters.parmStateId(_stagingTable.State);
        addressStringBuilderParameters.parmCityName(_stagingTable.City);
        addressStringBuilderParameters.parmStreet(_stagingTable.Street);
             
        addressing = LogisticsPostalAddressStringBuilder::buildAddressStringFromParameters(addressStringBuilderParameters);

        select firstonly partyAddressView
            where partyAddressView.Party == custTable.Party
            &&    partyAddressView.Address == addressing;

        if (partyAddressView)
        {
            postalAddressRecId = partyAddressView.PostalAddress;
        }
        else
        {
            postalAddress.clear();
            //postalAddress.initValue();
            postalAddress.Street           = _stagingTable.Street;
            postalAddress.City             = _stagingTable.City;
            postalAddress.State            = _stagingTable.State;
            postalAddress.ZipCode          = _stagingTable.ZipCode;
            postalAddress.CountryRegionId  = _stagingTable.CountryRegionId;

            newPartyAddressView.initFromPostalAddress(postalAddress);
            newPartyAddressView.Party = custTable.Party;
            newPartyAddressView.LocationName = _stagingTable.DeliveryName ? _stagingTable.DeliveryName : custTable.name();

            dirParty = DirParty::constructFromPartyRecId(CustTable.Party);                  
            roleIds = [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Delivery).RecId];

            newPartyAddressView = dirParty.createOrUpdatePostalAddress(newPartyAddressView, roleIds);
            postalAddressRecId = newPartyAddressView.PostalAddress;                                            
        }

        return postalAddressRecId;
    }


Go for a drive and revert with your question if any.

Happy DAXING...


No comments:

Post a Comment