CRM 2011 – How to set up a lookup using Javascript

[tweetmeme source=”BenHosk” only_single=false]

I haven’t had to use this javascript yet but I know I am going to need in the future, so I am going to blog about it so I can find it.

there are quite a few examples of this but this blog post compares it to CRM 4 as well, which is extra useful.

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

if you want some more examples and a discussion this forum discussion is the place to look

This page also had an interesting link to an article

Seven JavaScript Things I Wish I Knew Much Earlier In My Career

which I found interesting because I know very little about Javascript

17 thoughts on “CRM 2011 – How to set up a lookup using Javascript

  1. Metaphorix September 21, 2011 / 2:29 am

    it’s very difficult to compare javascript from CRM 4 and CRM 2011. My advice is just to learn how it works in CRM 2011 and get using it. You will find it better in the long run once you have learned how to use it.

    Like

  2. manmis April 20, 2012 / 3:13 am

    Short!

    Xrm.Page.getAttribute(“fieldName”).setValue([new {id: idValue, name: textValue, entityType: typeValue}]);

    Like

  3. titus June 18, 2012 / 12:52 pm

    can i know how to set range to price list to a product in sales navigation

    Like

  4. villerantala June 13, 2013 / 4:59 pm

    Hi, Can you help me please?

    “value[0].id = idValue;
    value[0].name = textValue;
    value[0].entityType = typeValue;”

    I didn’t get this part, id = i have to put the guid of the record?
    name = i have to put the name of what? entity? record?
    type = the user? systemuser? the entity :l

    I’m nuts and newbbie in this javascript for CRM…

    Thanks for the help.

    Like

    • aaa June 27, 2014 / 2:57 pm

      id = the guid of the record
      name = the text you usualy see in the lookup – (its the primary field of the record)
      entityType = the logical name of the entity (like – account or contact …)

      Like

  5. salah September 5, 2013 / 11:05 am

    thnx…it worked

    Like

  6. Prathamesh Powar July 10, 2017 / 11:18 am

    I have 14 records in the array, and i want to bind this array to the lookup control, how i can achieve it, please suggest

    Like

    • Hosk July 10, 2017 / 11:28 am

      You cannot bind records to a CRM control.

      If you want to bind arrays to controls you will need to create a new control and show it in an HTML page and Iframe

      Like

Leave a comment

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