How to Get and Set a Lookup Field Using JavaScript in Dynamics 365

Adam Murchison, 20 September 2017

When setting a lookup value using JavaScript in Microsoft Dynamics 365, the value you use has to be an array and the values in that array must be of type object. Within the object the required components are the id, logical name and the entity type.

Retrieving a lookup field through JavaScript can be a little more difficult than expected, it is important to note that using business rules you can access the values within a lookup and set other fields with this value. Regardless, there are some cases where you might want to use JavaScript to get a lookup field value.

This is a simple example:

The lookup value retrieved is an object and to get each component is shown in the image above. This is the main difference between a regular field e.g. text field and a lookup field, a text field will simply return the text value where the lookup field returns an object. Some small but important components are required when getting the field value. These are that the field is on the form and that the field doesn’t return null, so it’d be wise having a null check.

Setting a lookup value:

Alternative way of setting a lookup value with same result: