jump to navigation

Setting a default value on a lookup field in Microsoft Dynamics CRM 4.0 October 27, 2009

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

d4 logo I got a request to set the “Unit” on an Opportunity Product to always default to “Primary unit” when a product is created. Setting default values on lookup fields seems to be a common request, so i decided to post the code to achieve this. This is an Onload Event.

//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 = ‘{299F37C4-6D9D-4628-ADF9-9479A6F209BA}’;
   lookupItem.typename = ‘uom’;
   lookupItem.name = ‘Primary Unit’;
// Add the object to the array.
   lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
   crmForm.all.uomid.DataValue = lookupData;

If you want to use this code for other lookups, here is what you need to change:

//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.
   crmForm.all.FieldName.DataValue = lookupData;

Enjoy!!

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

Advertisements

Comments»

1. Heti Makwala - November 18, 2009

Hi
Thanks for the blog. Simplifies things for those of use who are not programmers.

I’ve created a custom entity called pg_rating which has a 1:N relationship to the Opportunity entity. There is a lookup field pg_ratingid lookup field on the Opportunity form. I would like the lookup field to default to the option that has the GUID as per the code below (it’s the pg-rating field) when the form loads. I’ve added the code (as below), please tell me why it’s not working.

//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 = “{7DE47436-DBCD-DE11-B61B-00155D000904}”;
lookupItem.typename = “pg_rating”;
lookupItem.name = “pg_rating”;
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
crmForm.all.pg_ratingid.DataValue = lookupData;

jarrettexpertcrm - November 18, 2009

Hi Heti,

Thank you for you comment. Looking at your code lookupItem.typename = “pg_rating” is that the value you want to default on in the look up field? Also before the field and entity name you should add new_
example: lookupItem.typename = “new_pg_rating”;

2. Heti Makwala - November 18, 2009

Hi

“pg_rating” is the name of the lookup field and the option for the lookup is “Thumbsuck”.
During the configuration, I replace “new” with “pg” for custom entities.

3. matt - April 15, 2011

thaqnks for the post, but applying this just makes the Unit field greyed out instead of populating the value. any thoughts?

jarrettexpertcrm - May 1, 2011

Hi Matt,

Not sure why it would be doing that. Can you paste the code you are using here so i can take a look

Sean - May 20, 2011

Hello, Can you please post a sample script for Price List in CRM 2011. I have created a price list called ZList and I’d like for it to be the default value when employee creates a new quote instead of them having to select. Not sure how to go about it. Thanks

jarrettexpertcrm - May 23, 2011

Hi Sean,

I will work on a solution for this and update my blog

-JC

jarrettexpertcrm - May 23, 2011

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: