What To Do With a Dynamics 365 Lookup OnChange Event Error

I recently came across an odd JavaScript error for the onChange event of a lookup field in Dynamics 365. Apparently, removing the lookup item from the field works fine if it’s being removed directly from the form, however, when removing it from the ‘Look Up Record’ dialog box (see below), a JavaScript error “Unable to get property ‘id’ of undefined or null reference” pops up.

clip image002 thumb What To Do With a Dynamics 365 Lookup OnChange Event Error

The same JavaScript onChange event is being called from the ‘Look Up Record’ dialog box and from the form. For the latter scenario, getValue method returns ‘null’ on removing the item from the lookup field but the same method returns an empty array when removing it from the ‘Look Up Record’ box. Hence, the JavaScript error pops up.

Therefore, if you encounter this error on a LookUp onChange event, make sure you’re checking the ‘null’ value for the first array element in your code as below:

clip image003 thumb What To Do With a Dynamics 365 Lookup OnChange Event Error

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog