Quite often there is a requirement to have the values of one picklist or lookup be determined by the value of another picklist or lookup. As most of you will know Microsoft CRM does not have the concept of sub picklists or sub lookups. I’ve often seen this requirement solved through the use of picklists and JavaScript. Yuk! This solution means that every time a new value is added to either the parent or child picklist, the JavaScript code needs to be changed. Not a great solution for system administrators who have no knowledge of JavaScript.
Here is a simple solution using lookups rather than picklists with some simple, generic (and warning: unsupported) JavaScript.
For my example today lets presume I need to add a Primary Industry lookup to the Account form. I also need add a Sub Industry lookup where the values shown are determined by what I select as the Primary Industry.
First I need to create two new entities, Primary Industry and Sub Industry. Next I need to create a 1:n relationship between the two new entities with Primary Industry as the Primary Entity. As you can see in the image below I have also set Primary Industry relationship attribute as Business Required so all Sub Industries must have a Primary Industry.
Next I add the Primary Industry attribute to the Sub Industry form.
In order to be able to filter the Sub Industry lookup based on the selected Primary Industry value I need to add the Primary Industry attribute as a find column in the Sub Industry Lookup View. I can now publish my two new entities.
I add some values to my Primary Industries and my Sub Industries.
Now I need to add the Primary and Sub Industry lookups to my Account form. I create a n:1 relationship between the Account entity and my Primary Industry and Sub Industry entities. In each case the Account Entity will be the Primary Entity. I can then add the created Primary Industry and Sub Industry lookup attributes to the Account form as shown below.
Now I need to add the JavaScript to filter the value of the Sub Industry lookup based on what is selected as the Primary Industry. On the change event of the Primary Industry lookup I add the following JavaScript:
var lookupItem = new Array;
lookupItem = crmForm.all.new_primaryindustryid.DataValue;
var searchVal = lookupItem[0].name;
crmForm.all.new_subindustryid.additionalparams = ‘search=' + searchVal;
I can now publish all my changes and test the result. As you can see below because I selected Finance as the value for my Primary Industry, when I click on the lookup for the Sub Industry it is “pre-filtered” and only shows the Sub Industries that relate to Finance.
10 comments:
H Karl,
Thanks for this information. I would like to do something which I imagined would be simple. On the Opportunities Tab i'd like to see the contact the opp' is for and what company they work for. I n that way on the lookup tab we can clearly see both bit's of info. however if I select a user as the primary contact how do I create a field that automatically fills in the comapny they work for? Is it similar to what you have detailed here?
Thanks Karl,
From what I have seen, this does not work with 4.0, that the additionalparams attribute does not work under 4.0. Whenever I try this, I do not get anything in the lookup for the subindustry. I also receive a javascript error on the page.
Thanks,
Jim
Got it working. Thanks.
Now the problem is that the Form Assistant does not display the filtered list. Any idea how to get it to display a filtered list just like the lookup?
Hi Jim,
Unfortunately this does not work in the form assistant nor does it work with automatic resolution.
This is beautifully simple!! Can you please explain why it is "unsupported"?
Hi Mark,
The reason its unsupported is because there is no reference to the additionalparams property in the SDK. Basically its only supported if its in the SDK. Now having said most of the fantastic solutions that people have blogged about aren't in the SDK :)
Hey Karl,
This is great.
I need to ask one thing how can u create a new lookup for existing entities and how to use the new lookup at whenever i need to
Hello Karl,
How come you do not have any steps to generate the mappings between the Account and both Industry(Primary & Sub)entities? I cannot add the Industry fields to the Account form because there are no Primary Industry and Sub Industry attributes under the Account form.
Thank you for documenting this procedure. It has been giving me headaches. If you could answer these it would help me out greatly!
I figured it out by creating a 1:N relationship between Account and both Industry fields and N:1 relationship between the Industries and the Account to get the lookup attribute on the Account form.
Thanks Karl, really it was a great help for me. But only one problem I am facing - if I write
field.lookupbrowse = 1;
Just to hide the search field then this script is not working.
Is there any solution for that.
If you can help in this reagrds then I will be thankful to you.
Post a Comment