How to Populate Multiple Fields Using Lookup Value in Dynamics 365
How to Populate Multiple Fields Using Lookup Value in Dynamics 365
In Dynamics 365, lookup fields are commonly used to establish relationships between records. A frequent business requirement is automatically populating related information when a user selects a lookup value.
For example, when a user selects a Customer lookup, the system can automatically populate fields such as Email, Phone Number, Address, and Account Number. This improves efficiency, reduces manual entry, and ensures data consistency.
Business Scenario
Suppose a Contact lookup is selected on a custom form. Once the contact is chosen, the following information should automatically populate:
- Email Address
- Mobile Number
- Job Title
- Company Name
Why Use Lookup-Based Population?
- Reduce manual data entry
- Improve user productivity
- Maintain data accuracy
- Enhance user experience
- Automate repetitive tasks
Register JavaScript on Lookup OnChange
Register a JavaScript function on the lookup field's OnChange event.
function populateFields(executionContext)
{
var formContext =
executionContext.getFormContext();
}
Get Selected Lookup Record
var lookup =
formContext.getAttribute
("parentcustomerid")
.getValue();
var recordId =
lookup[0].id.replace("{","")
.replace("}","");
Retrieve Related Data
Use Dataverse Web API to retrieve additional fields from the selected record.
Xrm.WebApi.retrieveRecord( "contact", recordId, "?$select=emailaddress1, mobilephone,jobtitle" )
Populate Form Fields
formContext.getAttribute( "new_email") .setValue(result.emailaddress1); formContext.getAttribute( "new_phone") .setValue(result.mobilephone); formContext.getAttribute( "new_jobtitle") .setValue(result.jobtitle);
Complete Process Flow
- User selects lookup value.
- OnChange event triggers JavaScript.
- Lookup record ID is retrieved.
- Web API fetches related data.
- Multiple fields are populated automatically.
- User continues without manual entry.
Common Use Cases
- Customer information auto-fill
- Vendor details population
- Employee profile loading
- Project information retrieval
- Asset management forms
- Service request automation
Benefits
- Improved productivity
- Reduced data duplication
- Faster record creation
- Better user experience
- Higher data quality
Key Takeaways
- Lookup selections can automatically populate related fields.
- JavaScript and Dataverse Web API make automation simple.
- Reduces manual data entry and errors.
- Improves overall user productivity.
- Commonly used in Dynamics 365 form customizations.