POWEROBJECTS

718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

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.

customizing

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:

MethodFunctionality
addCustomViewAdds a new view for the lookup control
setDefaultViewSets the default view for the lookup control
getDefaultViewReturns 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 = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                            "<entity name='contact'>" +
                                "<attribute name='contactid' />" +
                                "<attribute name='fullname' />" +
                                "<attribute name='emailaddress1' />" +
                                "<attribute name='telephone1' />" +
                                "<attribute name='telephone2' />" +
                                "<attribute name='address1_line1' />" +
                                "<attribute name='address1_line2' />" +
                                "<attribute name='address1_city' />" +
                                "<attribute name='address1_stateorprovince' />" +
                                "<attribute name='address1_postalcode' />" +
                                " <order attribute='fullname' descending='false' />" +
                                "<filter type='and'>" +
                                    "<condition attribute='parentcustomerid' operator='eq' value='" + parentAccountid + "'  />" +
                                "</filter>" +
                              "</entity>" +
                        "</fetch>";

        // build Grid Layout     
        var layoutXml = "<grid name='resultset' " +
                        "object='1' " +
                        "jump='contactid' " +
                        "select='1' " +
                            "icon='1' " +
                            "preview='1'>" +
                            "<row name='result' " +
                            "id='contactid'>" +
                            "<cell name='fullname' " +
                            "width='200' />" +
                            "<cell name='emailaddress1' " +
                            "width='200' />" +
                           "<cell name='telephone1' " +
                            "width='100' />" +
                            "<cell name='telephone2' " +
                            "width='100' />" +
                            "<cell name='address1_line1' " +
                            "width='150' />" +
                            "<cell name='address1_line2' " +
                            "width='150' />" +
                            "<cell name='address1_city' " +
                            "width='150' />" +
                            "<cell name='address1_stateorprovince' " +
                            "width='100' />" +
                            "<cell name='address1_postalcode' " +
                            "width='100' />" +
                            "</row>" +
                            "</grid>";

        // 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!

Avatar for Joe D365

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.