Friday, October 30, 2009

Adding A Filtered Lookup In CRM

by Danny Varghese 02.03.09

Another common question I see in blogs from users are is there a way to add filtered lookups? That is only allow users to "lookup" certain records that related to that particular one. Here's a real life example:

Say you have an account that has a 1:N parental relationship with entity A. Now let's say there's another entity B that has N:1 referential relationships with both account, and entity A. There is a way on entity B so that after you select, on the lookup, a record of the account you want to relate to it, to filter the second lookup of entity A to only those related to the account you just selected. i.e.

Parental Relationship

Account --> Entity A

Referential Relationship

Entity B -- Account

Entity B -- Entity A

With simple JavaScript, when a user selects the lookup value for the account, the lookup for Entity A can be filtered to point to only those entity A's that are related to the account you just chose. Here's the code to place on the form of Entity B:

crmForm.all.<lookup field for entity A>.additionalparams = 'search=' + encodeURIComponent(crmForm.all.<lookup field for account>.DataValue[0].name);

That's it! Simple, yet effective.

No comments: