CRM Blog

Adding a secondary lookup to a Microsoft CRM Form

by Darren Liu 05.24.07

Darren Liu

Crowe Chizek and Company LLC

May 24th 2007

http://www.crowecrm.com

Summary: Darren Liu shows how to add secondary lookup's to Microsoft CRM forms.

There have been many people in the Microsoft CRM Community asking how to add a secondary lookup to the Account and Contact form. Due to an inherent limitation in Microsoft CRM 3.0, we are not able to create another relationship between an account or contact entity.

To add another lookup, we need to use JavaScript to inject some code into the CRM form. The way that I am showing here will not create a relationship between the entities. Instead, it will store the Name and the GUID with the record. I used JavaScript to add a lookup field to the form. In this sample, I will add a secondary contact lookup to an Account.

Pros: You will able to create the lookup field and also able to use the CRM lookup box. You can create as many as you want.

Cons: If the name changed for the contact, it will not update the name that is stored in the account entity. If you want to update the name, you need to write a post-callout to update the name in the account record.

Here are the steps to create a secondary lookup:

Step 1: Create two custom attributes in the Account Entity.

If you have set a different prefix for custom attributes you will have to modify the code. In the example code below our prefix in the Microsoft CRM system is new.

Display Name: Secondary Contact

Schema Name: new_secondarycontactname

Type: String

Description: Stores the Contact name

Display Name: Secondary Contact Value

Schema Name: new_secondarycontactvalue

Type: String

Description: Stores the GUID for the Contact

Step 2: Add two newly created attributes to the account form.

Hide the label for "Secondary Contact Value" field. We will use JavaScript to hide the "Secondary Contact Value" field later.

Step 3: Insert the following code to the Account form onLoad event.

The code below will replace the Secondary Contact text field with a Lookup box.

crmForm.all.new_secondarycontactvalue.style.visibility="hidden";

customSecondaryLookup("new_secondarycontactname"); } catch(e)

{

alert(e.description); } }}

function customSecondaryLookup(displayFieldName)

{ {

Try

{

var fld = crmForm.elements[displayFieldName];

var temp = fld.value;

if (temp.length == 0)

{

fld.insertAdjacentHTML("afterEnd","<table class='lu' cellpadding='0' cellspacing='0' width='100%' style='table-layout:fixed;'><tr><td><div class='lu'>&nbsp;</div></td><td width='25' style='text-align: right;'><img src='/_imgs/btn_off_lookup.gif' id='" + displayFieldName + "' class='lu' lookuptypes='2' lookuptypenames='contact:2' lookuptypeIcons='/_imgs/ico_16_2.gif' lookupclass='BasicCustomer' lookupbrowse='0' lookupstyle='single' defaulttype='0' req='0'></td></tr></table>");

}

else

{

fld.insertAdjacentHTML("afterEnd","<table class='lu' cellpadding='0' cellspacing='0' width='100%' style='table-layout:fixed;'><tr><td><div class='lu'><span class='lui' onclick='openlui()' oid='" + temp + "' otype='2' otypename='contact'><img class='lui' src='/_imgs/ico_16_2.gif'>" + crmForm.all.new_secondarycontactvalue.value +"</span></div></td><td width='25' style='text-align: right;'><img src='/_imgs/btn_off_lookup.gif' id='"+ displayFieldName +"' class='lu' lookuptypes='2' lookuptypenames='contact:2' lookuptypeIcons='/_imgs/ico_16_2.gif' lookupclass='BasicCustomer' lookupbrowse='0' lookupstyle='single' defaulttype='0' req='0'></td></tr></table>");

}

fld.parentNode.removeChild(fld);

fld = crmForm.elements[displayFieldName];

fld.value = temp;

Step 4: Insert the following code to the Account form onSave event.

The code below will strip out the GUID for the Contact from the return lookup HTML and save it to the Secondary Lookup Value field.

var S_CONTACT_NAME = crmForm.all.new_secondarycontactname
.parentElement.parentElement.firstChild.innerText; crmForm.all.new_secondarycontactvalue.value = S_CONTACT_NAME;

Step 5: Save the Account Form.

Step 6: Publish the Account Form.

Conclusions

As you can see from the screen below we are now able to add and use secondary lookups. Note that this type of modification is not supported by Microsoft therefore be sure to take appropriate care before using this type of customizatfr.

Secondarylookup

Darren Liu is a Microsoft CRM Specialist with Crowe Chizek and specializes in software development and data integration using tools such as .NET and BizTalk.

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Security Check
Please answer the simple math problem below.

(required)