The JavaScript is a better tool while developing User Interface in Dynamics CRM. The out of the box facilities may not be sufficient at certain cases. In such sophisticated scenarios JavaScript is a better candidate to figure it out.
Setting field values based on condition is a very common practice in Dynamics CRM. For all other field types except Lookup field values can be directly set using conventional practice. But for Lookup type of fields the Name, GUID of record and Entity name must be given to set a lookup field.
In our case what we need to achieve is to set a lookup field based on the selection of a particular field.

We need to populate ‘Unit of Measure’ field automatically based on the selection in the ‘Item’ field.
‘Item’ is a lookup type column from Item table and ‘Unit of Measure’ is also a lookup type column from UOM table.
Since both the fields are of type lookup, the getting and setting of values is more complicated. Now let’s try to understand the Java Script in detail.
Understanding the Script
Initially if the Item field contain data, get the value of Item field using getAttribute method and obtain the GUID of the corresponding record.
if (Xrm.Page.getAttribute(“eng_itemid”).getValue() !== null){
var itemid =formContext.getAttribute(“eng_itemid”).getValue();
var id = itemid[0].id;
Here ‘eng_itemid’ is the schema name of Item column in corresponding table.
Now get the matching record with the GUID stored in variable id from the Item table and select the column which holds the GUID of Unit of Measure value. In our case the schema name of the column is ‘_eng_baseuomid_value’ which is obtained from REST API method.
The following code snippet can be used to obtain the mentioned attributes.
Xrm.WebApi.retrieveRecord(“eng_item”, id, “?$select=_eng_baseuomid_value”).then(
function success(result) {
var uomidlookup = result._eng_baseuomid_value;
The variable ‘uomidlookup’ holds the GUID of Unit of Measure value.
If uomidlookup’ contain data, get the name from related table which is UOM.
Xrm.WebApi.retrieveRecord(“eng_uom”, uomidlookup, “?$select=eng_name”).then(
function success(results) {
}
Now set an array to store the name, GUID and Entity type.
var lookup = [];
lookup[0] = {};
lookup[0].id = uomidlookup;
lookup[0].name = results.eng_name;
lookup[0].entityType = “eng_uom”;
The variable ‘lookup’ is an array variable which hold the name, GUID and Entity type.
Now in order to set the ‘Unit of Measure’ field, follow the following method.
formContext.getAttribute(“eng_unitofmeasureii”).setValue(lookup);
Where ‘eng_unitofmeasureii’ is the schema name of ‘Unit of Measure’ column.
JavaScript Code:
function unitOfMeasure(executionContext){
// Getting Form Context
var formContext = executionContext.getFormContext();
var transferId = formContext.data.entity.getId();
if (Xrm.Page.getAttribute(“eng_itemid”).getValue() !== null){
var itemid =formContext.getAttribute(“eng_itemid”).getValue();
// Getting the GUID of the lookup record
var id = itemid[0].id;
Xrm.WebApi.retrieveRecord(“eng_item”, id, “?$select=_eng_baseuomid_value”).then(
function success(result) {
var uomidlookup = result._eng_baseuomid_value;
if (uomidlookup !== null){
Xrm.WebApi.retrieveRecord(“eng_uom”, uomidlookup, “?$select=eng_name”).then(
function success(results) {
var lookup = []; // // Creating a new lookup Array
lookup[0] = {}; // new Object
lookup[0].id = uomidlookup; // GUID of the lookup id
lookup[0].name = results.eng_name; // Name of the lookup
lookup[0].entityType = “eng_uom”; // Entity Type of the lookup entity
if (formContext.ui.getFormType() === 1 || formContext.ui.getFormType() === 2)
{
formContext.getAttribute(“eng_unitofmeasureii”).setValue(lookup);
} });
}
else {
formContext.getAttribute(“eng_unitofmeasureii”).setValue(null);
}},
function (error) {
Xrm.Utility.alertDialog(error.message;
});
}
else {
formContext.getAttribute(“eng_unitofmeasureii”).setValue(null); }}
Add the JavaScript web resource and call the function in the onChange property of Item field.
To see how to add JavaScript code as web resource and call the function, follow the link Dynamics 365 form level notification using JavaScript – Dynatecon Solutions

It is clear from the above image that on change of Item value, Unit of Measure is also changing automatically
Hope this helps!.