Setting a CRM 4.0 Lookup field in JavaScript

It seems that the days are getting busier as I’m finalizing a couple of projects and preparing for TechEd 2010. Still, I want to still try and drop at least a couple of trinkets off here so you’ll keep reading this blog.

So what is today’s pearl of wisdom? I get asked all the time how to set the value of a lookup field in CRM via JavaScript.

It’s not quite as straightforward as setting the value of a textbox or picklist, but it’s not terrible. Basically, it boils down to a few steps.

The first thing to remember is that the value of a lookup field is an array of objects. My understanding is that by default you can’t actually create a CRM lookup field that accepts multiple values. However, a good example of this is in the CRM email entity’s “To” or “CC” fields. These fields allow you to set multiple recipients for an email.

The second thing to remember is that despite the fact that you can’t set multiple values, you can set a single value but that value is a complex type, an object with individual properties, not just an intrinsic type like a string or integer.

So how do we accomplish that in JavaScript? First, we’ll create an object:

var lookupObject = new Object;

Now that we have our object, we can set some values. Typically, you’ll want to set 3 main properties: ID, name, and typename. The ID is the GUID of the object whose value you’re setting. The name is the string that your users will see. Finally, the typename is the CRM entity name for the value you’re setting, in this case systemuser. You can set them like this: = "21EC2020-3AEA-1069-A2DD-08002B30309D"; = "John Doe";
lookupObject.typename = "systemuser";

OK, so now you’ve set the values for the object that you’ll be setting as the value of your lookup. As I mentioned though the value of a lookup field is an array of objects, even if you can only set a single value. So we can create the array like so:

var lookupObjectArray = new Array();

We can only set one object, so we’ll set our new object as the first member of our newly created array.

lookupObjectArray[0] = lookupObject;

Alright, now that we have our array and its first member is our object, let’s set the array as the lookup field’s DataValue:

crmForm.all.new_userid.DataValue = lookupObjectArray;

Finally, we want to make sure that our newly submitted value gets saved back to the database when we save the record, and we can accomplish that quite simply by setting ForceSubmit to true:

crmForm.all.new_userid.ForceSubmit = true;

There we have it! We created an object, a container array, set the appropriate values and assigned it to out lookup field. Here’s all the code together in one block:

var lookupObject = new Object;
var lookupObjectArray = new Array(); = "21EC2020-3AEA-1069-A2DD-08002B30309D"; = "John Doe";
lookupObject.typename = "systemuser";
lookupObjectArray[0] = lookupObject;
crmForm.all.new_userid.DataValue = lookupObjectArray;
crmForm.all.new_userid.ForceSubmit = true;



One thought on “Setting a CRM 4.0 Lookup field in JavaScript”

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s