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:


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)
  • 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.js library
  • 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

  1. Open a Contact Record or Create New record.
  2. 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

Copyright © 2025 Dynamics Services Group