In this webinar, our experts showcase a variety of demo use cases of how different components of the...
When implementing Microsoft Dynamics CRM, you may have a requirement where you need to see a selection of records filtered to only records that are associated with an earlier user selection. Some of these filters can be achieved using out-of-the-box customizations. But in some cases, you might need to achieve this by implementing some simple customizations in CRM.
Today we'll explain how to implement a custom filtered lookup using simple customizations. This can be used in any out-of-the-box or custom entity.
Let's say that in the Account entity, when a user selects a primary contact, you want to show the user only those contacts associated to the selected parent account as a default view. This can be achieved by using some JavaScript in CRM. The following Xrm.Page.ui control object methods provide us a way to implement custom filtered lookups for a lookup field in CRM:
Method | Functionality |
addCustomView | Adds a new view for the lookup control |
setDefaultView | Sets the default view for the lookup control |
getDefaultView | Returns the ID(GUID) value of the default view of the lookup control |
The code snippet below does the magic of creating the custom filtered lookup:
var availableDefaultViewId; // FUNCTION: formOnLoad function formOnLoad(parentAccountFieldName,primContactFieldName) { availableDefaultViewId = Xrm.Page.getControl(parentAccountFieldName).getDefaultView(); setPrimaryContactLookup(parentAccountFieldName, primContactFieldName, false); } // FUNCTION: setPrimaryContactLookup function setPrimaryContactLookup(parentAccountFieldName, primContactFieldName, resetSelection) { // Get the selected parent account Id var parentAccount = Xrm.Page.getAttribute(parentAccountFieldName).getValue(); if (parentAccount != null) { var parentAccountid = parentAccount[0].id; var parentAccountname = parentAccount[0].name; if (resetSelection == true) { // reset old selection for Procedure Code Xrm.Page.getAttribute(primContactFieldName).setValue(null); } // use randomly generated GUID Id for our new view var viewId = "{6fd72744-3676-41d4-8003-ae4cde9ac282}"; var entityName = "contact"; // give the custom view a name var viewDisplayName = "Associated contacts for " + parentAccountname + ""; // find all associated contacts of the selected parent account var fetchXml = "" + " "; // build Grid Layout var layoutXml = "" + " " + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "" + " " + " "; // add the Custom View to the primary contact lookup control Xrm.Page.getControl(primContactFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true); } else { // no parent account is selected, reset primary contact Lookup View to the default view Xrm.Page.getControl(primContactFieldName).setDefaultView(availableDefaultViewId); } }" + "
" + "" + " | " + " | " + " | " + " | " + " | " + " | " + " | " + " | " + " |
Here are the steps to implement the custom filtered lookup for the primary contact in the account form:
This is the form OnLoad event screen shot after calling the function:
After implementing all these steps, once you click on the lookup icon of the Primary Contact field, you should see the custom filtered lookup created for the Primary Contact as shown in the screenshot below.
We hope this was helpful! If you need assistance with anything related to Microsoft Dynamics CRM, we've got a great support team and we're happy to help.
Happy CRM'ing!
Hello,
I'm trying to modify this for the Cases Entity where I have an Account and Contact look-up. When the page loads I'm getting Field: Window Event: onload
Error:Unable to get value of the property 'GetDefaultView': object is null or undefined.
Basically I changed out the fields parentAccountFieldName,primContactFieldName throughout the script and if you click ok on the error and then click the lookup for contacts i get the right contacts but they aren't carrying over the ViewDisplayname(gonna guess this is part of the error). We are on crm 2011 rollup 14.
Any help would be great!!
Thanks!
clear check of "Pass execution context as first parameter"
Has anyone else noticed that the results do not line up with the column headers? This is the behaviour I am experiencing and it is also visible in the last screenshot above. Any way to fix this?
Noticed the same problem, but wasn't able to fix it in an amount of effort that was worth it. Anyone else have any luck?
http://www.magnetismsolutions.co.nz/blog/jaredjohnson/2014/07/20/dynamics-crm---secret-to-not-having-misaligned-columns-from-addcustomview
This link shows how to align the columns properly.
Forgot to say thanks! As this fix worked great for me!
This is wonderful!!! Helped me a lot. Did not need to even change much. Worked like a charm !!
Apparently this no longer works with CRM 2015? Could someone please verify?
Hi ,Thank you for sharing.But since it is a custom view we are creating with a random GUID ,it just sows the name of the view in the lookup and gives an error saying "savedquery With Id = 6fd72744-3676-41d4-8003-ae4cde9ac282 Does Not Exist"
Kindly help asap.
Hi Kimaya - Check that the view id has {} in the string such as "{xxxxx}". In crm 2015 this code needs to be adjusted a bit.
Also i want that if a company does not not have children companies, then the same parent company should appear in the new custom view.
A new problem which i am facing now is that when i save the form the value in the new field containing the custom filter disappears. I am using CRM 2011. Kindly help so that i can hold the value even after the form is saved.
Wanted to know how to get the count of the records fetched in the custom lookup.
Hi Joe, I implemented same way a custom view to one of filed in a form it is working fine if i select value from look up popup but when i trying to select by typing in the look up filed after selection it's throwing error as
Record Is UnavailableThe requested record was not found or you do not have sufficient permissions to view it.
And if i try to save i am getting error as
lookup mru item that you have selected hasn't been resolved yet
Is any one know the reason for the issue please reply to this post
Hi Joe, I added same way custom view to a look up it is working fine in IE but in other browsers it is not working at the addCustomView function calling by passing parameters it is returning error as
"Refused to set unsafe header "Content-Length" could you help me to resolve this