Home
Assigning default values to lookup fields
Category: Extending MS CRM | Posted on Wednesday, May 14, 2008

This solution works the same way on both MS CRM 3.0 and 4.0.

1. Open up your MS CRM in a browser.
2. Navigate to the entity you like to link to as a default value in your lookup field.
3. Click Ctrl + N to open up a new browser window showing the entity with its 32 digits plus 4 dashes Guid in the browsers address field. The Guid is the unique id of the entity.



4. Write down the Guid or save it to your clipboard.
5. You also need to know the name, type and typename of the entity you are linking to and also the attribute name of the lookup field you are assigning the value to.
6. Copy the below script to the onLoad event of the form that holds the lookup filed.
7. Remember to enable the script and publish the entity.

Source code
/* The crmForm.FormType == 1 makes sure your script only runs if it’s a new empty form. If you skip this part you’ll overwrite the lookup filed every time the form is opened up. */
if(crmForm.FormType == 1)
{
/* create a new array */
var newlookup = new Array(new Object());

/* assign values to your array*/
newlookup [0].id = "{ replace this with the Guid of the entity }";
newlookup [0].name = "replace this with the name of the entity";
/* every entity has a type number. 1 = account, 2 = contact est. You can see a list of all entities with their name and type at http:///Sdk/List.aspx. */
newlookup [0].type = replace with type number of the entity your are linking to;
newlookup [0].typename = "replace with typename of the entity your are linking to";

/* assign your array to the lookup field on your form. */
crmForm.all.< replace with lookup attribute name >.AddItems(newlookup);
}


How the final result might look like.



As you can see it’s not that hard to do and it can save lots of users much time filling out forms.

All source code on this website is provided as is. If you use any of my solutions or just parts of them I’m not to be held responsible for any errors or loss of data you might encounter.