Microsoft Dynamics CRM 2011 Jscript Walkthrough: Setting Up Look-Up Fields, Part I

By - November 6, 2012

I think one of the more common questions I receive when working with Jscript in Microsoft Dynamics CRM is, how do I set the value in a look-up field?

With the new format for the Jscript in Microsoft Dynamics CRM 2011 this can be somewhat tricky especially compared to how simple it was in Dynamics CRM 4.0.  I have had multiple customers say to me, “I found the piece of code on a blog site but it keeps throwing a object is null error.”  So I will provide the code for one specific example but also show you what needs to be changed to adapt it for other purposes.

Here is the magical code to set the value of a look-up field, in this example I am setting the default contract template for contracts.

function setcontracttemplate()


var template= Xrm.Page.getAttribute(“contracttemplateid.Value”);

if (template== null) {     var PLVal = new Array;

PLVal = Xrm.Page.getAttribute(“contracttemplateid”).getValue();

if (PLVal == null) {

var lookupFld = new Array();

lookupFld[0] = new LookupControlItem(“{F81ECE78-C5CD-DB11-B7C6-000F20DAD965}”,2011, “Service”);


}     }



So now that we have the code lets break it down.

The variable “template” should be the field that you are trying to set the value of.  In this case it is the contracttemplateid field on the contract.

var template= Xrm.Page.getAttribute(“contracttemplateid.Value”);

The variable PLVal is also going to be the field that you are trying to set the value of.

PLVal = Xrm.Page.getAttribute(“contracttemplateid”).getValue();

The lookupFld[0] variable is the value that you are trying to populate the field with, in this case we are trying to set the contract template field to service.

lookupFld[0] = new LookupControlItem(“{F81ECE78-C5CD-DB11-B7C6-000F20DAD965}”,2011, “Service”);

Let’s break down the values that are in the above variable:

  1. “{F81ECE78-C5CD-DB11-B7C6-000F20DAD965}” – This is the GUID of the value that you are trying to put into the look-up field.  You can grab the GUID out of the address bar for the Service Contract Template.
  2. 2011 – This is the entity type code for the value you are trying to put into the look-up field.  you can get this from the URL of that entity or a quick Google search will also work.
  3. Service –  This is the logical name of the entity

Once you have that all done correctly all you have left to do is set the value of the field with the following code.


In this case we are setting the value of the contracttemplateid field.

I hope this helps and clears up any confusion on the new syntax for setting the value of a look-up field in Microsoft Dynamics CRM 2011.  Next time I will dive into move involved Jscript and expose some of the advanced things you can do with scripting in Microsoft Dynamics CRM 2011.

If you are looking for assistance with Microsoft Dynamics CRM,  you can reach our professionals via email at or by phone at 800.274.3987.

By: John Annunziato, RSM –

Receive Posts by Email

Subscribe and receive notifications of new posts by email.