JavaScript Conversion: Creating a supported LookupControlItem method

In Dynamics CRM 4.0, you could use an unsupported internal function to create the value to be inserted into a Lookup control.  The code would look something like this:

var lookupItem = new Array();
lookupItem[0] = new LookupControlItem("{3A6C1B06-C62F-DC11-AFC2-0019B9B20373}", 1, "A Sales Store 3");
crmForm.all.pricelevelid.DataValue = lookupItem;

 

This function still exists in Dynamics CRM 2011 but it is still unsupported and quite honestly, doesn’t look like it works correctly. This is mostly due to the fact that using the Object Type Code, or the numeric identifier for the Entity, has been deprecated and no longer functions as it once did.  In CRM 2011, you must use the Entity’s Name instead.

So, where does that leave us?  Fortunately, in a not so bad place.

You can add the following function to a JScript web resource, add the web resource to your form, and modify your code a little, and it will function as it always did.  Here is the new function:

function LookupControlItem2011(id, typeName, name)
{
    var lookupItem = new Object();

    lookupItem.id = id;
    lookupItem.entityType = typeName;
    lookupItem.name = name;

    return lookupItem;
}

 

It is used like this:

var lookupData = new Array();
lookupData[0] = LookupControlItem2011('{B1FC92B0-B305-E211-B881-00155D00650B}', "account", 'A Sales Store 3');

Xrm.Page.getAttribute("parentaccountid").setValue(lookupData);

 

Three things had to change, to make this work correctly:

1. Remove the new keyword from in front of LookupControlItem.

2. Rename LookupControlItem to LookupControlItem2011, which will prevent a conflict with the existing function.

3. Replace the 2nd parameter, which is an integer, with the schema name for the Entity.

As you can see from my examples, I replaced 1 with account.

 

Making these small changes will allow you to use your existing code with a minimum amount of effort.

Leave a Reply