Introduction
A common requirement is automatically populating a Contact’s address fields when an Account or another Contact is selected in a lookup.
Why Automate Address Population?
Manually copying addresses is error-prone and time-consuming. By automating it, we:
✔ Reduce data entry errors
✔ Save time for users
✔ Ensure consistency across records
The Solution: A Clean JavaScript Web Resource
Here’s the optimized script we’ll break down:
const ADDRESS_FIELDS = [
"address1_line1",
"address1_line2",
"address1_line3",
"address1_city",
"address1_stateorprovince",
"address1_postalcode",
"address1_country"
];
const PROGRESS_INDICATOR = {
MESSAGE: "Please wait..."
}
async function onAccountNameChange(executionContext) {
try {
Xrm.Utility.showProgressIndicator(PROGRESS_INDICATOR.MESSAGE); // It is almost not needed
const formContext = executionContext.getFormContext();
const accountField = formContext.getAttribute("parentcustomerid");
if (!accountField?.getValue()?.[0]?.id) return;
const lookupValue = accountField.getValue()[0];
const entityId = lookupValue.id.replace(/[{}]/g, '').toLowerCase();
const fieldsToSelect = ADDRESS_FIELDS.join(',');
const record = lookupValue.entityType === "contact"
? await Xrm.WebApi.retrieveRecord("contact", entityId, `?$select=${fieldsToSelect}`)
: await Xrm.WebApi.retrieveRecord("account", entityId, `?$select=${fieldsToSelect}`);
if (record) {
updateAddressFields(formContext, record);
}
} catch (error) {
console.error("Error in onAccountNameChange:", error.message);
// Consider Xrm.Navigation.openErrorDialog(error.message); for user feedback
} finally {
Xrm.Utility.closeProgressIndicator();
}
}
function updateAddressFields(formContext, addressRecord) {
ADDRESS_FIELDS.forEach(fieldName => {
const field = formContext.getAttribute(fieldName);
if (field && addressRecord[fieldName] !== undefined) {
field.setValue(addressRecord[fieldName]);
}
});
}
How to Implement This in Dynamics 365
1. Create a JavaScript Web Resource
- Navigate to Power Apps -> Solutions -> Create New/Or use old one
- click + New -> More -> Web resource
- Set the following properties:
- Name:
new_Contact_OnChange_AddressName.js - Display Name: Conatct OnChange AdressName
- File Type: JavaScript (JS)
- Name:
- Paste the provided JavaScript code (see below) or upload the js file
- Click Save

2. Add the Script to a Form
- Open the form where phone validation is needed (e.g., Contact or Account)
- Go to Form Libraries → + Add library (add the js – web resouce ) (see the image)
- Click on the Phone field. Click on Events. Add an OnChange event for the Address Name field (
perentcustomerid) - Select the
new_Contact_OnChange_AddressName.jslibrary - Set the function name to
onAccountNameChange - Check “Pass execution context as first parameter” to yes
- Click Done and Save and publish

3.
Testing the Implementation
- Open a Contact Record or Create New record.
- Select a Account Name
- Expected: The location fields (address fields) will be set up automatically

Conclusion
By using modern JavaScript techniques, we’ve created a clean, maintainable, and efficient solution for auto-populating addresses in Dynamics 365. This approach:
✔ Reduces manual work
✔ Improves data accuracy
✔ Follows best practices
Keep Learning!
- Share your thoughts or questions in the comments below.
- Stay tuned to our blog for more Dynamics 365 tips and tutorials.
Thanks for reading!

Leave a comment