Sunday, July 24, 2011

MSCRM 4.0: Showing Only Related Contacts in Primary Contact Lookup in Account Form

A client asked: We have many accounts with multiple contacts. When we want to change the primary contact, the lookup shows all contacts, not just those related to the account. How can we change this?

To display only contacts with the current account as their parent account, you need to make some customizations.

There are two parts to making this work:
Customize your Contact entity
* Select > Lookup view > click on 'Add Find Columns'
* Check 'Parent Customer' > Save changes > Publish.

Following this change, when you do a lookup for primary contact, you can search by parent company name as well as the person's name. This is useful on its own.

To complete the customization and do this lookup automatically:
* Customize the Account entity main form > edit the form properties
* Add an onLoad event with the following line of code:

crmForm.all.primarycontactid.additionalparams = 'search=' +
crmForm.all.name.value;

This adds a parameter to the primarycontactid field to pass to the search box when the lookup is invoked (the parameter is the field "name" which is the Account Name of the current Account).

* Save and close > Publish the Account entity.

Now when you use the Primary Contact lookup from within the Account, you will only see contacts that are identified as working for that company (by their Parent Account).

Note: In Microsoft Dynamics CRM 2011, this functionality is automatically built-in to the lookup dialog.

No comments: