Stefan Pienaar
I would love to change the world, but they won't give me the source code

Changing the value of a Lookup field in CRM

June 26, 2009 11:46 by stefanpienaar

You can change the value of a Lookup Field on a CRM form (for example the owner or the selected currency) by using the following piece of javascript:

var lookupObject = new Object;
var lookupObjectArray = new Array();
lookupObject.id = '<Guid of the target record>';
lookupObject.name = '<Name of the target record>';
lookupObject.typename = "<target record type>";
lookupObjectArray[0] = lookupObject;
crmForm.all.<target field>.DataValue = lookupObjectArray;
crmForm.all.<target field>.ForceSubmit = true;


Just make sure to change the values between < and > to suit your own situation. For example if I wanted to change the default currency I would change the code to the following:

var lookupObject = new Object;
var lookupObjectArray = new Array();
lookupObject.id = '{EF63EA80-02BE-DD11-8241-0017A410DDDA}';    // Rand's Guid
lookupObject.name = 'Rand';                                    // Name of currency
lookupObject.typename = "transactioncurrency";                 // Lookup's type
lookupObjectArray[0] = lookupObject;
crmForm.all.transactioncurrencyid.DataValue = lookupObjectArray;
crmForm.all.transactioncurrencyid.ForceSubmit = true;


Note that the Guid used above will be unique per installation so yours won’t necessarily match mine.The last line just ensures that the record is updated correctly if my Lookup field is disabled on the form.


Categories: Microsoft CRM 4
Actions: E-mail | Permalink | Comments (0)