Set and Retrieve a Lookup inside Lookup Id and Value
In Dynamics 365, working with lookup fields is a common task, but when dealing with nested lookups (a lookup inside another lookup), it can become slightly challenging. This blog will walk you through an example of retrieving data from nested lookups and updating fields on the form using JavaScript. We'll also explore how to handle such scenarios in a professional and efficient way.
Understanding the Scenario
Let’s assume we have the following requirements:
1. Entities Involved:
- Department (as_departmenttable): Contains a lookup to a Manager (as_manager) entity.
- Manager (as_manager): Contains fields such as fullname, contactid, etc.
2. On the form, when a user selects a Department, the system should automatically populate the Manager field using the lookup from the selected Department.
3. Additionally, the Manager field itself could have other related lookups that might need further processing.
JavaScript Code using Xrm.WebApi
Below is the JavaScript function to retrieve the Manager for a selected Department and populate the Manager field on the form. The function is designed to handle scenarios where data is retrieved from nested lookups.
function populateManagerBasedOnDepartment(executionContext) {
var formContext = executionContext.getFormContext();
var departmentField = formContext.getAttribute("as_departmenttable");
if (departmentField && departmentField.getValue() !== null) {
var departmentId = departmentField
.getValue()[0]
.id.replace("{", "")
.replace("}", "")
.toLowerCase();
Xrm.WebApi.retrieveRecord(
"as_departmenttable",
departmentId,
"?$select=_as_manager_value&$expand=as_manager($select=firstname,lastname,contactid,fullname)"
).then(
function success(result) {
if (result != null) {
var managerId = result._as_manager_value;
var fullname = result.as_manager.fullname;
var managerLookupValue = [
{
id: managerId,
name: fullname,
entityType: "as_manager",
},
];
formContext.getAttribute("as_manager").setValue(managerLookupValue);
} else {
formContext.getAttribute("as_manager").setValue(null);
console.log("No manager found for the selected department");
}
},
function error(error) {
console.log(
"Error while retrieving Department's Manager: " + error.message
);
}
);
} else {
formContext.getAttribute("as_manager").setValue(null);
}
}
Key Steps in the Code
1. Retrieve Form Context: The function begins by retrieving the formContext using the executionContext. This allows us to access and manipulate fields on the form.
2. Check for Department Value: Before making any API calls, the code verifies that the Department field has a value.
3. Retrieve Data Using Web API:
- Uses Xrm.WebApi.retrieveRecord to fetch the selected department's record.
- The $select parameter fetches the _as_manager_value (the lookup field pointing to the manager).
- The $expand parameter retrieves detailed fields (like fullname) from the related Manager entity.
4. Update the Manager Field: - If a manager is found, the code sets the Manager field with a lookup object containing id, name, and entityType.
- If no manager is found, the Manager field is cleared, and a message is logged to the console.
5. Error Handling: Any errors during the Web API call are logged to the console for debugging purposes.
Conclusion
Working with nested lookups in Dynamics 365 may seem complex, but with the right approach, it can be streamlined effectively. The combination of JavaScript and the Web API allows you to retrieve and process data from related entities dynamically, enhancing the automation and functionality of your forms.
By following the practices outlined in this blog, you can handle even the most complex lookup scenarios with confidence.
Hope this helps!.
- Sachin Sen
https://www.linkedin.com/in/sachin-sen-5b48091b9/