March 3, 2016

CRM 2016 Update lookup field bug

I encountered a strange behavior after upgrading on premise CRM 2015 to 2016 version. 

In the account form after I set the value of the custom lookup field (lookup to custom Country entity) I got javascript error: 

SCRIPT5007: Unable to get property 'trim' of undefined or null reference

After some hours of debugging I removed all javascript code. Then I copied the example from SDK, the error was still there.

Investigating the setValue method I discovered, that something like "internalOnChange" was called. I suspect this method was called because there are some filtered lists in the page that depend on the value of this field.

Long story short, there are two ways to fix this:

Method 1:
Go to Settings-Administration-System settings. All the way down at the end of the page enable "Use legacy form rendering".

This will definitely slow down form loading.

Method 2:
The code deep down in the out-of-the-box CRM javascript libraries the script wants to parse the attribute type, but it is not there. The value should be "lookup" and you should pass it in the object when calling setValue.

//Create the lookup value object
var lookupReference = [];
lookupReference[0] = {};
lookupReference[0].id = "{a0c68c89-9752-e311-93f9-00155d78043f}";
lookupReference[0].entityType = "new_entityname";
lookupReference[0].name = "Display name";

//Magic happens here
lookupReference[0].type = "lookup";
    
//Update the lookup field   
Xrm.Page.getAttribute("new_entitynameid").setValue(lookupReference);

No comments: