Thursday, September 6, 2007

Getting values from a lookup control

We recently needed to capture both the GUID and the text value from a CRM lookup control. I found some quick code to do this on the following newsgroup:

http://groups.google.com/group/microsoft.public.crm/browse_thread/thread/9bc6721768f0a1f1/4b5f5d0ff5972dc0?lnk=st&q=ms+crm+get+lookup+datavalue&rnum=1&hl=en#4b5f5d0ff5972dc0



var lookupItem = new Array;

lookupItem = crmForm.all.primarycontactid.DataValue;

if (lookupItem[0] != null)
{
// The text value of the lookup.
alert(lookupItem[0].name);


// The GUID of the lookup.
alert(lookupItem[0].id);
}


Quickly getting the GUID of the current form

Here is a quick snippet of javascript code to get the GUID of the current CRM form:

crmForm.ObjectId