Saturday, May 21, 2011

How to: Get and Set a Lookup Value in Jscript in Microsoft Dynamics CRM 2011

This is one of those posts that comes from the fact that there are a lot of questions in the forums that relate to this topic and there are a lot of good posts that demonstrate either how to set a lookup value or get a lookup value, but there aren't many that show both.  So that is what we will do today.

Here is a quick demo on how to either get or set a lookup value in Jscript in Microsoft Dynamics CRM 2011:

 //Get a lookup value 
    var lookupItem = new Array(); 
    lookupItem = Xrm.Page.getAttribute("yourAttributeSchemaName").getValue();
    if (lookupItem != null) 
        var name = lookupItem[0].name; 
        var guid = lookupItem[0].id; 
        var entType = lookupItem[0].entityType;

 //Set a lookup value
    var value = new Array();
    value[0] = new Object();
    value[0].id = idValue;
    value[0].name = textValue;
    value[0].entityType = typeValue;
 //or alternatively you can set it like this
   Xrm.Page.getAttribute("yourAttributeSchemaName").setValue( [{id: idValue, name: textValue, entityType: typeValue}]);

Note: You must wrap your code in a function in CRM 2011 to be called by an event handler.  This is achieved by using the syntax below:

function test()
   //my jscript code here!

Now you can just call your function name from an event handler by specifying the web resource and the function name "test".

I hope this helps!



  1. Slight correction:

    It should be " if (lookupItem !== null) "

    != does a different check than !==, and if you try lookupItem[0] it attempts to resolve the array before verifying null, thus producing an error.

    1. Or I could just check for lookupitem != null instead of lookupitem[0] != null.

      You make a good point. I will fix it.

  2. Hello,

    If i also want to fetch other attributes (fields) of an entity of lookup. How can i do that ?

    1. Once you have the ID you can use a retrieve request


  3. The question not answered here is how do you retrieve the ID in idValue? value[0].id = idValue;

    1. Does this not work?
      var guid = lookupItem[0].id;

    2. The code you referenced was setting a value, not retrieving.

  4. This comment has been removed by the author.

  5. I appreciate the blogging you do. Maybe you can point me down the right path on this one.

    I have two fields on my Case form: One is a lookup to Teams, the other is a lookup to users. On the Team form, I have a field called 'pw_primaryactionee'
    (also a lookup) that may or may not be populated with a User value.

    The idea is that when I select a TEAM on the case form, the 'Actionee' value (also on the case form) populated if the 'Primary Actionee' field on the team form is populated.

    That all works. The issue that I'm having is that AFTER I SAVE THE CASE, the value that *is* populated into my "pw_actionee" field on the case form is gone. (note: the function 'teamChange' is called on the onChange event of the team lookup field on the case form. 'setFieldReq' is just a function I'm calling from a home-grown library to set a field as Business Required)

    Code snips below. Any ideas?

    function teamChange() {
    context = Xrm.Page.context;
    serverUrl = context.getServerUrl();
    var theTeam = Xrm.Page.getAttribute("pw_cc").getValue();
    retrieveRecord(theTeam[0].id, "TeamSet", retrieveSuccessfulTeam, null);

    function retrieveSuccessfulTeam(Data, textStatus, XmlHttpRequest) {
    if (Data != null) {
    var arrACT = new Array();
    arrACT[0] = new Object();
    arrACT[0].id =
    arrACT[0].entityType = Data.pw_PrimaryActionee.LogicalName;
    arrACT[0].name = Data.pw_PrimaryActionee.Name;
    else {
    var ctlACTIONEE = Xrm.Page.getControl("pw_actionee");
    var atrACTIONEE = ctlACTIONEE.getAttribute();

  6. Never mind. I'm just looking at the forest and can't see the trees again...

    "arrACT[0].id =" should have been
    "arrACT[0].id = Data.pw_PrimaryActionee.Id"

    stupid case-sensitive silly languages...

    Thanks for your patience as I bash my head into the walls to lean my lessons...

  7. Hi,

    I am trying to retrieve Owner's Team in MSD CRM 2011 and set it to lookup Owner's Team attribute.

    Can you guide me with the script for the same.

  8. Task goes like dis ...

    1)Take contact entity then in contact form create a lookup field with name contact type lo students,employee's etc ..
    2)Now again open another entity like school in school form create field contact..lookup
    3)When you select contact.. dani type untadi kada.. adi automatic ga select ipovali USING JAVA SCRIPT