Get lookup values in Model driven app using Xrm.WebApi
One of my project, i had a requirement to get the Lookup value by passing the lookup value from the form when page load. For that i have added a JavaScript function on page load methos by adding the javascript file in the web resources of the app.
In the JavaScript method, i have added following code the get the Lookup value using the existing lookup value id
function GetValuesFromLookup(executionContext) {
var formContext = executionContext.getFormContext();
var transferId = formContext.data.entity.getId();
if (formContext.getAttribute("student").getValue() !== null) {
var itemid = formContext.getAttribute("student").getValue();
var id = itemid[0].id;
Xrm.WebApi.retrieveRecord("student", id, "?$select=_classid_value").then(
function success(result) {
var uomidlookup = result._classid_value;
if (uomidlookup !== null) {
formContext.getAttribute("classid").setValue(null);
} else {
formContext.getAttribute("classid").setValue(null);
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
});
} else {
formContext.getAttribute("classid").setValue(null);
}
}
In the code above i am passing student Id values to get the class id lookup value. once we retrieve the class id populating it to the form control.
Just to remember to get the lookup value we need to pass the webapi method parameter as _LookupColumnName_Value.
Comments