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.

About these ads

About Rhett Clinton MVP
Dynamics CRM MVP

12 Responses to Set Lookup Value using JavaScript Tip

  1. Daniel Cai says:

    Hi Rhett,

    Actually I had the exact same thought the other day…

    Cheers,
    Daniel

  2. Not a bad article.
    There is something to think about.

    Sincerely yours, administrator of the site Tips and Tricks

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

  4. Mitch says:

    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);
    }

  5. Andy Cronk says:

    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.

    • 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

  6. Garcia says:

    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

  7. Garcia says:

    I found that for ti to work is a must to change the entityType: “typeValue” for enityName. Cheers hope it helps some one

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: