-
Dynamically Populate Lookup Fields
JavaScript
Dynamically Populate Lookup Fields
Write a function to dynamically fill lookup fields based on selections in other fields, improving user efficiency by reducing manual entry.
// Dynamically Populate Lookup Fields
function populateLookupField(executionContext) {
const formContext = executionContext.getFormContext(); // Retrieve the form context from the execution context in Dynamics 365
const Guid = "e35d1cf1-501f-4443-8a4f-1972fc665ad5"; // Define a random GUID to be assigned to the lookup field
const lookupField = formContext.getAttribute("lookupField"); // Retrieve the lookup field (lookupField) from the form
// Set the value of the lookup field with an object containing the reference details
lookupField.setValue([{
id: Guid, // Unique identifier of the referenced entity (GUID)
name: "Related Name", // Display name shown in the lookup field
entityType: "entityName" // Logical name of the entity associated with the GUID (e.g., "account" or "contact")
}]);
}