web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Update Customer Address Using Code Not Showing Address on Customer Address Screen

(1) ShareShare
ReportReport
Posted on by 6
Customer address not showing on the Customer Address screen after updating the customer address using code? Need Help on this.

Below is the code am using

public boolean createCustomerAddressManualV2(
    CustAccount _custAccount,
    ENDShopifyCustomerAddressContract _addressDetails)
{
    CustTable                       custTable;
    DirParty                        dirParty;
    DirPartyPostalAddressView       postalAddressView;
    DirPartyPostalAddressView       savedPostalAddressView;
    DirPartyLocation                partyLocation;
    DirPartyLocationRole            partyLocationRole;
    LogisticsLocationRole           invoiceRole;
    LogisticsLocationRole           businessRole;
    LogisticsAddressCountryRegion   countryRegion;
    container                       roles = conNull();
 
    RecId                           existingLocationRecId = 0;
    boolean                         success = false;
 
    str                             address1;
    str                             address2;
    str                             city;
    str                             stateProvince;
    str                             zipCode;
    str                             countryCode;
    str                             street;
 
    // -----------------------------
    // 1. Basic validation
    // -----------------------------
    if (!_addressDetails)
    {
        error("Address details contract is null.");
        return false;
    }
 
    custTable = CustTable::find(_custAccount, true);
 
    if (!custTable.RecId)
    {
        error(strFmt("Customer %1 not found.", _custAccount));
        return false;
    }
 
    address1      = strLRTrim(_addressDetails.parmAddress1());
    address2      = strLRTrim(_addressDetails.parmAddress2());
    city          = strLRTrim(_addressDetails.parmCity());
    stateProvince = strLRTrim(_addressDetails.parmProvince());
    zipCode       = strLRTrim(_addressDetails.parmZip());
    countryCode   = strUpr(strLRTrim(_addressDetails.parmCountryCode()));
 
    if (!address1)
    {
        error(strFmt("Address line 1 is required for customer %1.", _custAccount));
        return false;
    }
 
    if (!city)
    {
        error(strFmt("City is required for customer %1.", _custAccount));
        return false;
    }
 
    if (!countryCode)
    {
        error(strFmt("Country/region is required for customer %1.", _custAccount));
        return false;
    }
 
    street = address1;
    if (address2)
    {
        street = strFmt("%1, %2", address1, address2);
    }
 
    // -----------------------------
    // 2. Resolve country/region
    //    Supports either:
    //    - exact D365 CountryRegionId
    //    - ISO code if available in your environment
    // -----------------------------
    select firstOnly countryRegion
        where countryRegion.CountryRegionId == countryCode;
 
    if (!countryRegion.RecId)
    {
        select firstOnly countryRegion
            where countryRegion.ISOCode == countryCode;
    }
 
    if (!countryRegion.RecId)
    {
        error(strFmt(
            "Country/region '%1' is not configured in D365FO for customer %2.",
            countryCode,
            _custAccount));
        return false;
    }
 
    // -----------------------------
    // 3. Prepare roles
    //    Billing address => Invoice + Business
    // -----------------------------
    invoiceRole  = LogisticsLocationRole::findByType(LogisticsLocationRoleType::Invoice);
    businessRole = LogisticsLocationRole::findByType(LogisticsLocationRoleType::Business);
 
    if (invoiceRole.RecId)
    {
        roles += [invoiceRole.RecId];
    }
 
    if (businessRole.RecId)
    {
        roles += [businessRole.RecId];
    }
 
    if (conLen(roles) == 0)
    {
        error("No address roles were found. Please verify Logistics location roles setup.");
        return false;
    }
 
    // -----------------------------
    // 4. Find existing PRIMARY INVOICE postal address
    //    so we update instead of creating duplicates
    // -----------------------------
    if (invoiceRole.RecId)
    {
        select firstOnly partyLocation
            where partyLocation.Party           == custTable.Party
               && partyLocation.IsPostalAddress == NoYes::Yes
               && partyLocation.IsPrimary       == NoYes::Yes
            join partyLocationRole
                where partyLocationRole.PartyLocation == partyLocation.RecId
                   && partyLocationRole.LocationRole  == invoiceRole.RecId;
 
        if (partyLocation.RecId)
        {
            existingLocationRecId = partyLocation.Location;
        }
    }
 
    // -----------------------------
    // 5. Create/update through DirParty
    // -----------------------------
    try
    {
        ttsBegin;
 
        dirParty = DirParty::constructFromCommon(custTable);
 
        postalAddressView.clear();
        postalAddressView.initValue();
 
        postalAddressView.Party           = custTable.Party;
        postalAddressView.Location        = existingLocationRecId; // 0 = create new, otherwise update existing
        postalAddressView.LocationName    = "Billing Address";
        postalAddressView.Street          = street;
        postalAddressView.City            = city;
        postalAddressView.State           = stateProvince;
        postalAddressView.ZipCode         = zipCode;
        postalAddressView.CountryRegionId = countryRegion.CountryRegionId;
        postalAddressView.IsPrimary       = NoYes::Yes;
 
        savedPostalAddressView = dirParty.createOrUpdatePostalAddress(postalAddressView, roles);
 
        if (!savedPostalAddressView.Location)
        {
            throw error(strFmt(
                "Address framework did not return a location for customer %1.",
                _custAccount));
        }
 
        ttsCommit;
 
        info(strFmt(
            "Billing address %1 successfully for customer %2.",
            existingLocationRecId ? "updated" : "created",
            _custAccount));
 
        success = true;
    }
    catch (Exception::DuplicateKeyException)
    {
        ttsAbort;
        error(strFmt(
            "Duplicate address/location detected while processing billing address for customer %1.",
            _custAccount));
        success = false;
    }
    catch (Exception::Error)
    {
        ttsAbort;
        error(strFmt(
            "Failed to create/update billing address for customer %1.",
            _custAccount));
        success = false;
    }
 
    return success;
}
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    239,289 Most Valuable Professional on at
    Moved from Integration, Dataverse, and general topics forum to Finance | Project Operations, Human Resources, AX, GP, SL forum.
     
    Please tell us what you've found when you debugged your solution. Do you mean that your code completed successfully and you see the data in database but not in a particular form?
  • SH-24040842-0 Profile Picture
    6 on at
    I figured it out myself its working fine now. However, 
     
    Address data was inserted into the tables successfully, but it was not showing on the customer screen under the Address tab.
    Address Update Issue.jpg
  • SH-24040842-0 Profile Picture
    6 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 688

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 592 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 301

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans