Working with CRM Form Lookup Controls Programmatically


Lately, I have been asked a number of times about how to programmatically work with Lookup controls on CRM forms.  Basically, you can read values from and set new values to almost all of the CRM form controls, including Lookups.  You can write Jscript code that runs on CRM OnLoad/OnSave/OnChange form Events and manipulate the values of form controls programmatically.  Among lots of useful benefits of our form (a.k.a. client side) programming model, JScripts are particularly useful for data validation and setting default values (other examples are accessing rich functionality hosted on a web service as shown in one of my earlier blogs).     


 


BTW: We do have some of this info already documented in the SDK docs  and we will update the next refresh with all of these samples.   I thought I give you a headstart given the number of questions and emails I received.  


 


Here are some samples, one for read and one for write:


 


//Reading values from a Lookup control named primarycontatctid on the Account form


//----------------------------------------------------------------------------------------------


 


var lookupItem = new Array;


 


// This will get the lookup for the attribute primarycontactid on the Account form.


lookupItem = crmForm.all.primarycontactid.DataValue;


 


// If there is data in the field, show it in a series of alerts.


if (lookupItem && lookupItem[0] != null)


{


    // The text value of the lookup.


    alert(lookupItem[0].name);


 


    // The entity type name.


    alert(lookupItem[0].typename);


 


    // The GUID of the lookup.


    alert(lookupItem[0].id);


 


    // The entity type code of the lookup: 1=account, 2= contact.


    alert(lookupItem[0].type);


}


 


//Setting values to pricelevelid lookup control on the Opportunity form. 


//In this example, the price level is set to Retail.  


//------------------------------------------------------------------------------


 


//Create a lookupItem to store the values that you want to set to a target Lookup control


var lookupItem = new Array();


 


// Values on the signature of LookupControlItem are: GUID of pricelevel, type code of pricelevel and finally the name of the lookup value


lookupItem[0] = new LookupControlItem ("{F31BB38A-0EC0-403F-99A6-3AF469D7D76E}", 1022, "Retail");


 


//Set the form control value to the lookupItem just created


crmForm.all.pricelevelid.DataValue = lookupItem ;


 


BTW this code also works for CustomerId fields on the form (a common question:-))


 


Happy coding:-)


 


Comments (13)
  1. Arashs says:

    Thanks to Alex from Intellagentsolutions.com, I have updated the sample to cover for the case when the lookup is empty.

  2. Randy says:

    I have a question, on top of reading the values from a Lookup control, can I read other attributes of the Lookup control?

    For example, the primarycontactid lookup is from ANOTHER entity named "Contacts", which have other attributes like "First Name" and "Last Name". Can I read these values using JScript?

    Thanks.

  3. Arashs says:

    In order to read more attributes of the entity that lookup is pointing to, you need to make a web service call to get thoes attributes.  Making a Retreive call should give you what you need and you can do it either throuhg JScript or in a web service that your JScript calls into.

  4. Kaminda says:

    I have no words to express how much great work is this. I been searching for this for few days and finally you took me out of it.

    THANKS A LOT

    🙂

  5. Hello JScript friends,

    I am back from vacation and back again to enjoy you with some interesting…

  6. Newbee says:

    The blog is great but my only problem is when on any screen even f one of the lookUp values are entered from the form assistant then the Onchange event of any other Lookup field is not triggered.

    Any help on this is appreciated.

  7. Arashs says:

    Newbee

    What form and field do you see that behaviour on?  generally onchange events should fire no matter if the value is changed directly or via form assistant.  Feel free to email me directly.

  8. Vincent says:

    You saved my day!

    Excellent :)))

  9. Hello JScript friends, I’m back from vacation and back again to enjoy some interesting jscript stuff

  10. Jim says:

    Wow, that saved me ton of time

    Thanks!!!

  11. Leo Lopes says:

    How can I set null values to a lookup control?

Comments are closed.

Skip to main content