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:

Why Use Lookup-Based Population?

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

  1. User selects lookup value.
  2. OnChange event triggers JavaScript.
  3. Lookup record ID is retrieved.
  4. Web API fetches related data.
  5. Multiple fields are populated automatically.
  6. User continues without manual entry.
Important: Always validate that the lookup contains a value before calling the Web API to avoid JavaScript errors.

Common Use Cases

Benefits

Key Takeaways