Search code examples
javascriptdynamics-crm

Dynamics365 "LOOKUP CONTROL ERROR: Cannot add item of typename= to the lookup control" JavaScript Error


We just inherited a MS Dynamics 365 CRM project. We've been making some progress, but it's definitely not our forte.

We've got a form that pre-populates a number of fields. The JavaScript function "setLookupValue" is called to pre-populate the fields. This function is called frequently throughout the solution. It works in all other instances, except for this one field. When the function is called for this particular field, we're receiving the following JavaScript error:

"LOOKUP CONTROL ERROR: Cannot add item of typename= to the lookup control".

Here's the JS function:

setLookupValue: function (LookupId, Type, Id, Name) {
    var lookupReference = [];
    lookupReference[0] = {};
    lookupReference[0].id = Id;
    lookupReference[0].entityType = Type;
    lookupReference[0].name = Name;
    alert("SET LOOKUP DEBUG: Name = " + Name + ", Type = " + Type + ", ID = " + Id + ", LookupId = " + LookupId);
    Xrm.Page.getAttribute(LookupId).setValue(lookupReference);
}

I feel like the JS function is solid, since it's working in numerous places throughout the solution. This leads me to think it's something related to the parameters (data) being passed in?

I've done a bunch of research online, but am having a tough time getting this sorted.

Thanks in advance for any assistance!


Solution

  • Thank you to everyone who provided helpful insight. I was able to fix the issue by closely examining the JS lookup calls that were working.

    Best I can tell, what "LOOKUP CONTROL ERROR: Cannot add item of typename= to the lookup control” really means is that Dynamics cannot find an entity type matching the name you're passing in.

    In my case, I was passing in "affiliate". Due to an inconsistency in the way the entities were named in this solution, there was a prefix on the Affiliate entity name. The correct entity name needed for the function is displayed on the Entity Definition page.

    So ultimately, the entity name being passing in to the JS function for Type was incorrect. Once I added the prefix to correct the type name, the lookup call started working.