Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

Posted on by Microsoft Employee

I have implemented some JavaScript into one of my entities to auto-fill a lookup field. It is on the opportunity entity so can't use a work flow to do this. 

Basically, I want to auto fill a lookup field called 'Supplier' on my 'Commission Service' entity when the lookup field 'Existing Product' is filled in - it pulls the supplier from the entity 'Product' which is lookup 'Existing Product'.

I know that the first part of my JS is working as I am able to print out the response onto the console - but I am getting an error:

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

Also, it is not actually doing the job as the supplier field isn't filled in.

Has anyone come across this before? Or can anyone spot the reason why I seem to be getting this error in my code?

My code is below:

function existingProductOnChange() {
  var response = Xrm.Page.getAttribute("productid").getValue();
  var responseId = response[0].id;
  console.log("Testing Response:" + responeId);
  getSupplier(responseId);
}


function getSupplier(responseId){
  var req = new XMLHttpRequest();
  var url = "/api/data/v8.0/products()?$select=_new_supplier_value";
  req.open("GET", Xrm.Page.context.getClientUrl() + url, false);
  req.setRequestHeader("OData-MaxVersion", "4.0");
  req.setRequestHeader("OData-Version","4.0");
  req.setRequestHeader("Accept", "application/json; charset=utf-8");
  req.setRequestHeader("Prefer", "odata.include-annotations=\'*\'");
        req.onreadystatechange = null;
          console.log("TESTING 1");
        if (this.status === 200){
          var result = JSON.parse(this.response);
          var supplierID = result["_new_supplier_value"]
          var supplierName = result["_new_supplier_value@OData.community.Display.V1.FormattedValue"];
          console.log("TESTING 2");
          SetLookupField("new_supplier", supplierID, supplierName, "new_supplier");
          console.log("TESTING 3");
        }
        else {
          Xrm.Utility.alertDialog(this.statusText);
          console.log("TESTING 4");
        }
      }
    };

  req.send();
}


function SetLookupField(fieldName, lookupId, lookupName, entityName){
    var lookupData = new Array();
    var lookupItem = new Object();
    lookupItem.id = lookupId;
    lookupItem.name = lookupName;
    lookupItem.entityType = entityName;
    lookupData[0] = lookupItem;
    Xrm.Page.getAttribute(fieldName).setValue(lookupData);
}


*This post is locked for comments

  • Sinisa Kezic Profile Picture
    Sinisa Kezic 20 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    "LOOKUP CONTROL ERROR: Cannot add item of typename = to the lookup control" sometimes happens when entityType that is to be added programmatically is not allowed to be assigned even with UI  to the field.

    E.g.: If you are adding custom entity reference to the lookup value of activity:regardingobjectid make sue that custom entity is enabled for activities:

    or, the error will appear.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    If the above doesn't work, you can try this-

    ==========

    var supplierId = result._new_supplier_value;

    var supplierName = result["_new_supplier_value@OData.Community.Display.V1.FormattedValue"];

    =============

    You may refer the blog -

    community.dynamics.com/.../get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Hi,

    I don't think you need to use the ".id" & ".name" while fetching the id & name from the response.

    Try changing the following lines and see if it works-

    ========

    var supplierId = result["_new_supplier_value"];

              var supplierName = result["_new_supplier_value@OData.Community.Display.V1.FormattedValue"];

    ========

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Thanks for the responses, really helpful! Feel like I am almost there now with this, I've edited my code now (please see below), but now I have a new error. It is not able to get the result when I pass it through JSON? 

    Any suggestions would be much appreciated!! 

    My console is showing: 

    46617456.Capture.PNG

    Current code:

    function existingProductOnChange(){
      var response = Xrm.Page.getAttribute("productid").getValue();
      var responseId = response[0].id;
      var responseName = response[0].name;
      var responseEntityType = response[0].entityType;
      console.log("TESTING!! Response ID: " + responseId + ", Response Name: " + responseName + ", Response Entity Type: " + responseEntityType);
      getSupplier(responseId);
    }
    
    function getSupplier(responseId){
      console.log("TESTING!! In the getSupplier function.");
      var req = new XMLHttpRequest();
      var url = "/api/data/v8.0/products()?$select=_new_supplier_value";
      req.open("GET", Xrm.Page.context.getClientUrl() + url, false);
      req.setRequestHeader("OData-MaxVersion", "4.0");
      req.setRequestHeader("OData-Version", "4.0");
      req.setRequestHeader("Accept", "application/json");
      req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
      req.onreadystatechange = function() {
         if (this.readyState === 4) {
            req.onreadystatechange = null;
            console.log("TESTING!! ready state is 4");
            if (this.status === 200) {
              console.log("TESTING!! status is 200");
               var result = JSON.parse(this.response);
               console.log("TESTING!! Result: " + result);
               var supplierId = result["_new_supplier_value"].id;
               var supplierName = result["_new_supplier_value@OData.Community.Display.V1.FormattedValue"].name;
               console.log("TESTING!! Supplier ID: " + supplierId);
               console.log("TESTING!! Supplier Name: " + supplierName);
               // new_supplier is the lookup entity name
               SetLookupField("new_supplier", supplierId, supplierName, "competitor");
             }
             else{
               Xrm.Utility.alertDialog(this.statusText);
               console.log("TESTING!! The status was not 200");
             }
           }
         };
    req.send();
    }
    
    function SetLookupField(fieldName, fieldId, value, fieldType){
        var object = new Array();
        object[0] = new Object();
        object[0].id = fieldId;
        object[0].name = value;
        object[0].entityType = fieldType;
        Xrm.Page.getAttribute(fieldName).setValue(object);
    }
  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Hi ,

    You need to check below line . Seems you put both entity name and field name is same.

    SetLookupField("new_supplier(This should be field ID)", supplierID, supplierName, "new_supplier(This should be Entity Name which for which data is showing , for example country entity for country lookup)"); //

    //Make sure all the id name should be name not schema name (Always small character)

    
    
  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Can you check your fields names and entity names are correct/spelled correctly?

    Is the lookup field called new_supplier?

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Hi,

    Yes, it does look like your code is not correct for setting the lookup value.

    ==========

    var object = new Array();

    object[0] = new Object();

    object[0].id = fieldId;

    object[0].name = value;

    object[0].entityType = fieldType;

    Xrm.Page.getAttribute(fieldName).setValue(object);

    ===============

    community.dynamics.com/.../javascript-set-lookup-details

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: LOOKUP CONTROL ERROR: Cannot add item of typename = to the look up control

    Hey,

    when you settings the lookup i dont think you are accesing the position of the array.

    follow something like this.

    var value = new Array(); //create a new object array

    value[0] = new Object();

    value[0].id = id; // set ID to ID

    value[0].name = name; //set name to name

    value[0].entityType = “contact”;

    Xrm.Page.getAttribute(“from”).setValue(value); //set the value.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans