Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Creating a Custom Filtered Lookup in CRM 2011

Post Author: Joe D365 |

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:

  1. Add the above code snippet as a web resource in CRM.
  2. Navigate to Settings > Customization.
  3. Launch the working solution, or if you do not have one, either create one or click on Customize the System.
  4. Navigate to the account entity for which you want to add this custom filtered lookup.
  5. Access the Account form.

    custom filtered lookup in CRM 2011

  6. In the form that opened, double click on the Form Properties button at the top.

  7. In the popup that opens up, add the web resource you have created in step 1.

  8. Go to the form OnLoad event, click Add, and call the formOnLoad function by passing the parent account and primary contact field names as parameters.

    This is the form OnLoad event screen shot after calling the function:

  9. Go to the OnChange event of the Parent Account field and call the setPrimaryConatactLookup function by passing the parent account and primary contact field names, true as parameters.

  10. Save the changes.
  11. Publish customizations.

    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.

custom_filtered_lookup_last_image

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!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

16 comments on “Creating a Custom Filtered Lookup in CRM 2011”

  1. 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!

  2. 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?

    1. 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?

  3. 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.

    1. 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.

  4. 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.

  5. 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.

  6. 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

  7. 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

PowerObjects Recommends