jump to navigation

Setting a default value on a lookup field in Microsoft Dynamics CRM 2011 May 23, 2011

Posted by jarrettexpertcrm in Microsoft CRM General.
Tags: , , , , , , , , , , , , , , ,
trackback


In previous a post i posted how to default a value on a lookup field in 4.0. Here is the solution for CRM 2011
function priceList()
{
//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
lookupItem.id = ‘{27457CD-3C7C-E011-8E52-1CC1DE7983EB%257d}’;
lookupItem.typename = ‘pricelevel’;
lookupItem.name = ‘test’;
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupData);
}

In my example i am getting the pricelist “test” and defaulting it in the pricelist field on the Opportunity entity. Below is the template you can use to utilize the code for a different look up and values.
function priceList()
{
//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
lookupItem.id = ‘{Fields Guid}’;
lookupItem.typename = ‘Entity Name’;
lookupItem.name = Lookup Value;
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
Xrm.Page.getAttribute(“FieldName”).setValue(lookupData);

}

Enjoy!!

For more information about Dynamics Four you can contact us at http://www.dynamics4.com

Comments»

1. Sean - May 23, 2011

Thanks a lot. that’s really great job.

2. sony - September 28, 2011

Hi there,

In your above code snippet,

lookupItem.id = ‘{Fields Guid}’;
what should be the Guid value..??? Is it the Id of the lookupfield (to which we assigning the value).

Because in my case, im trying to default the lookup field with the values retrieved from entity that the same lookup field referring.

my lookup field name is currentaddress and im taking the city value from it.

Any help???
thanq

jarrettexpertcrm - October 6, 2011

Hi Sony,

You would want to find the GUID for whatever value it is you want to display in the lookup

-Jarrett

3. Steve Kessler - October 6, 2011

This is great but I do not know where to put this code. I am sorry if this should be clearer to me but it is not. Any help is greatly appreciated.

Thanks,
Steve

jarrettexpertcrm - October 6, 2011

Hi Steve,

You would want to put it OnLoad

– Jarrett

4. Steve Kessler - October 6, 2011

Thank you. I am trying figure out where the code would go. I need to make the library and from my understanding I need to make a web resource. I get how to do that. I am unsure what parameters to use including file type for this code. I appreciate you help with this and sorry if I am asking a stupid question.

Thanks,
Steve

jarrettexpertcrm - October 6, 2011

Create a jscript webresource, call it what you want, but for the function you will put “priceList”. You will then add that webresource to the onload of whatever entity you are working with.

– Jarrett


Leave a comment