Below is a simple JavaScript code to achieve that.
- On the forms OnLoad event write the following code.
FilterLookup = function(source, target)
{
if (IsNull(source) || IsNull(target)) { return; }
var name = IsNull(source.DataValue) ? '' : source.DataValue[0].name;
target.additionalparams = 'search=' + name;
}
- On the OnChange event of the Parent Account lookup mention the below code.
FilterLookup(crmForm.all.parentaccountid, crmForm.all.primarycontactid);
Note : Before you can see the code working, check the Parent Customer checkbox in the Contact Lookup View. Steps ->
- Settings > Customization > Customize Entites > Contact > Forms & Views > Contacts Lookup View
- Click on Add Find Columns on the right
- Tick Parent Customer
- Click OK and Publish
The Contact lookup opens(as shown in the figure below) and you can see "A Bike Store" in the search box & CRM is only returned those Contact records whose Parent Customer is "A Bike Store."
I'm still trying to figure out CRM and JavaScript. Your code above has some pseudo-code, I assume? Could you fill it out by providing a real-world code example?
ReplyDeleteI am not skilled enough to know what is required JavaScript and what I am supposed to replace with code relevant to my implementation.
This is a very real world code example. Please follow the exact steps as mentioned in the post to filter Primary Contact lookup based on Parent Account Value.
ReplyDeleteDon't forget to tick "Event is Enabled" check box on the OnLoad and OnChange events.
Some basic knowledge of JavaScript is required to understand this code.
In a nutshell
1. On the OnLoad event of the Account form copy this code. Tick Event is enabled check box.
FilterLookup = function(source, target)
{
if (IsNull(source) || IsNull(target)) { return; }
var name = IsNull(source.DataValue) ? '' : source.DataValue[0].name;
target.additionalparams = 'search=' + name;
}
2. On the OnChange event of the Parent Account field, add this code. Tick Event is enabled check box.
FilterLookup(crmForm.all.parentaccountid, crmForm.all.primarycontactid);
3. Tick Parent Customer check box for the Add Find Columns in Contacts Lookup View.
4. Publish Account and Contact entity.
5. Open a new Account form > Select a Parent Account from the lookup and see Primary Contact Lookup filter records based on the selected Parent Account.
Thank you so much. I apologize for my lack of understanding. I thought that the "source" and "target" references were to be replaced with something else.
ReplyDeleteThis really boosted my understanding of JavaScript in CRM!
Could this be used in "both directions"? Could you use the code _again_ on the contact lookup to constrain the parent account lookup at the same time? This way, if you knew one or the other you could constrain the other lookup. (I know the company, just show me contacts in that company. I know the person, now only show me the accounts this contact is associated with.)
I hope that made sense.
Of course, you can! In that case you'll swap the source and target fields. I mean the caller will be:-
ReplyDeleteFilterLookup(crmForm.all.primarycontactid, crmForm.all.parentaccountid);
You'll paste the above code on OnChange event of Primary Contact field.
You'll tick the Primary Contact Field check box on Account Lookup View -> Add Find Columns
Great Solution..thax.
ReplyDelete