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;
}