Set Lookup Value using JavaScript Tip

Everyone has seen the method of updating a CRM lookup field on a form, most examples look something like this.

// CRM 4
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;

crmForm.all.fieldName.DataValue = value;

// CRM 2011
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].entityType = typeValue;

Xrm.Page.getAttribute("fieldName").setValue(value);

How about doing it on one line like this instead.


// CRM 4
crmForm.all.field.DataValue = [{id: idValue, name: textValue, typename: typeValue}];

// CRM 2011
Xrm.Page.getAttribute("fieldName").setValue( [{id: idValue, name: textValue, entityType: typeValue}]);

There is a short but interesting article about JavaScript called Seven JavaScript Things I Wish I Knew Much Earlier In My Career by Christian Heilmann if you have some spare time to read.

12 thoughts on “Set Lookup Value using JavaScript Tip

  1. Pingback: CRM 2011 – How to set up a lookup using Javascript « Hosk's Dynamic CRM 2011 Blog

  2. Mitch

    I apologize for this code but, I am stuck with the new format after having used the conversion tool and the information on this and other sites. I previously could grab the lookup value of another entity from a parent entity. The code is trying to get the parentcustomerid from the contact table to fill in the Householdunitid field on a new entity.

    ///Get lookup ID

    {
    var lookupfield = Xrm.Page.getAttribute("new_ownerid").getValue();
    if (lookupfield != null && lookupfield [0] != null)
    {
    var householdlookupvalue = lookupfield [0].id;
    }
    else
    {
    var householdlookupvalue = " ";
    }
    }

    // Prepare variables for a contact to retrieve.
    var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

    // Prepare the SOAP message.
    var xml = ""+
    ""+
    authenticationHeader+
    ""+
    ""+
    "contact"+
    ""+lookupfield [0].id+""+
    ""+
    ""+
    "parentcustomerid"+
    ""+
    ""+
    ""+
    ""+
    "";

    // Prepare the xmlHttpObject and send the request.
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);

    // Capture the result.
    var resultXml = xHReq.responseXML;

    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {

    }
    // Display the retrieved value.
    else
    {

    //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();

    //Create an Object add to the array.
    var lookupItem= new Object();

    //Set the id, typename, and name properties to the object.
    lookupItem.id = resultXml.selectSingleNode("//q1:parentcustomerid").nodeTypedValue;
    lookupItem.entityType = 'account';
    lookupItem.name = resultXml.selectSingleNode("//q1:parentcustomerid").getAttribute("name");
    // Add the object to the array.
    lookupData[0] = lookupItem;

    // Set the value of the lookup field to the value of the array.
    Xrm.Page.getAttribute("new_householdunitid").setValue(lookupData);
    }

    Reply
  3. Andy Cronk

    Excellent stuff, small point on CRM 2011. To set the values in script on one line
    Xrm.Page.getAttribute(“fieldName”).setValue( [{id: idValue, name: textValue, entityType: typeValue}]); didn’t work for me I had to use
    Xrm.Page.getAttribute(“fieldName”).setValue( [{id: “idValue”, name: “textValue”, entityType: “typeValue”}]);
    Only worked with quotes for me, and works perfectly.

    Reply
    1. Rhett Clinton Post author

      Ok yes, I didn’t make it clear. You need ” quotes if you are passing in text values but you can also use variables in which case you do not need them.

      Thanks,
      Rhett

      Reply
  4. Garcia

    I use the code supplied like this:

    Xrm.Page.getAttribute(“responsiblecontactid”).setValue([{id: results[0].attributes[“primarycontactid”].guid, name: results[0].attributes[“primarycontactid”].name, entityType: results[0].attributes[“primarycontactid”].type}]);

    when this is parsed on the page it display the name of the lookup value but without showing the related entity icon and also it dosent let me save the form.

    any clue on this particular issue

    Reply
  5. Pingback: CRM 2011 – How to set up a lookup using Javascript ! « Srikanth Reddy's Blog

  6. Pingback: How to set up a lookup using Javascript | Web Fada Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.