public void findOrcreatePostalAddress(
LogisticsAddressStreet _street,
str _countryRegionCode,
str _city,
str _state,
str _postalCode,
LogisticsDescription _locationName,
PurchTable _purchTable)
{
LogisticsPostalAddress postalAddress;
DirPartyLocation partyLocation;
DirPartyPostalAddressView dirPartyPostalAddressView;
DirParty dirParty;
CompanyInfo companyInfo = CompanyInfo::findDataArea("USMF"); // Get company info for legal entity
// Try to find an existing postal address for this company with given parameters
select firstonly dirPartyPostalAddressView
where dirPartyPostalAddressView.Party == companyInfo.RecId
&& dirPartyPostalAddressView.LocationName == _locationName
&& dirPartyPostalAddressView.Street == _street
&& dirPartyPostalAddressView.City == _city
&& dirPartyPostalAddressView.State == _state
&& dirPartyPostalAddressView.CountryRegionId == _countryRegionCode;
// If no existing postal address found, create a new one
if (!dirPartyPostalAddressView)
{
// Construct DirParty from the company
dirParty = DirParty::constructFromCommon(companyInfo);
// Define location role container (e.g., Delivery)
container roleContainer = [LogisticsLocationRole::findByType(LogisticsLocationRoleType::Delivery).RecId];
// Set postal address details
dirPartyPostalAddressView.Street = _street;
dirPartyPostalAddressView.City = _city;
dirPartyPostalAddressView.CountryRegionId = _countryRegionCode;
dirPartyPostalAddressView.State = _state;
dirPartyPostalAddressView.ZipCode = _postalCode;
dirPartyPostalAddressView.IsPrimary = NoYes::No;
dirPartyPostalAddressView.LocationName = _locationName;
// Create or update the postal address in D365FO
dirPartyPostalAddressView = dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView, roleContainer);
}
}