From the CRM blogs, many people asked how to add a secondary lookup to the Account and Contact form? Due to CRM 3.0's limitation, we are not able to create another relationship between account to contact or contact to account.
To add another lookup, we need to use JavaScript to inject some codes to 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 fied to the form. In this sample, I will add a secondary contact lookup to an Account.
Pro: You will able to create the lookup field and also able to use the CRM lookup box. You can create as many as you wanted. :=)
Con: If the Name changed for the contact, it will not update the name that store in the account entity. If you want to upda the name, you need to write a postcallout 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.
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.
Take out 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'> </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 stripe 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.
Happy Programming!