Dynamics crm 2015 through error while setting value in a field

1.2k views Asked by At

Dyanamics crm 2015 sp 1, throw exception "Unable to get property 'trim' of undefined or null reference" While setting a lookup value.

  • CustomerId have all three properties i.e Id,Name and LogicalName
  • Field "new_customerprofileid" is populated too,but setValue() function throw error
  • Error location is in global.ashx, there is a line b.trim which through the error.

Code:

var Entity = RetrieveEntityById(Id, "SalesOrder");
if (Entity != null) {
    var CustomerId = Entity.CustomerId;
    if (CustomerId != null)
        if (Xrm.Page.getAttribute("new_customerprofileid") != null)
            Xrm.Page.getAttribute("new_customerprofileid").setValue([{ id: CustomerId.Id, name: CustomerId.Name, entityType: CustomerId.LogicalName }]);
}

One fix is to place line( .setValue("") ) in try catch block.

 var Entity = RetrieveEntityById(Id, "SalesOrder");
    if (Entity != null) {
        var CustomerId = Entity.CustomerId;
        if (CustomerId != null)
            if (Xrm.Page.getAttribute("new_customerprofileid") != null)
                try {
                    Xrm.Page.getAttribute("new_customerprofileid").setValue([{ id: CustomerId.Id, name: CustomerId.Name, entityType: CustomerId.LogicalName }]);
                } catch (ex) { }

    }

Please answer if anybody reach to some other fix for this error.

2

There are 2 answers

1
Risadinha On

The system tries to trim the input values but at least one of them was null and not a String. I guess this means that one of the values provided in the setValue dictionary is null: either CustomerId.Name or CustomerId.LogicalName. You might exclude those data sets or you might want to set their value to an empty string.

0
MeyC On

I've received this same problem. In my case, native CRM .js is trying to access a property named "type" on the lookup value that is being set, and then calling .trim on it resulting in b.trim is undefined. "type" is not a property that the documentation says needs to be set, so I suspect a bug in the CRM javascript, though trying to track down exactly why it is occurring has been difficult.

I've tried setting the property "type" to the object type code of the entity, however this seems to cause other problems that have been equally difficult to figure out.

As I am setting the id, name, and entityType, I've found that catching the exception has been the best solution thus far. I will most likely submit a ticket to MS to take a look at the issue.