Custom Lookups Filters

Just a very quick post to demonstrate how you can enhance the custom lookup filtering capabilities of the humble crm 2011 lookup.

In previous versions (Crm 3, 4.0) there were several published tricks to create filtered and custom lookups.  These , in general , were unsupported and wouldn’t upgrade.

This approach uses the addCustomView method. We tend to use this when the standard filters don’t offer us the flexibility.

The code snippet simply demonstrates how we can display values in a lookup based on another field. In this instance, an entity contains matching rules for a commission type. The calling entity only needs to choose matching rules based on the selected commission type.

  1. function setMatchRuleLookup(lookupFieldName, resetSelection) {
  2.  
  3.     if (resetSelection == true) {
  4.         // reset old selection for Matching rull
  5.         Xrm.Page.getAttribute(lookupFieldName).setValue(null);
  6.     }
  7.  
  8.     // Get the commission type – optionsetvalue
  9.     var commissionType = Xrm.Page.getAttribute("t4a_commissiontype").getValue();
  10.  
  11.     // use randomly generated GUID Id for our new view
  12.     var viewId = "{1DFB2B35-B07C-55D1-867D-258DEEBB87E2}";
  13.     var entityName = "t4a_matchingrule";
  14.  
  15.     // give the custom view a name
  16.     var viewDisplayName = "Matching rules for this commmission type";
  17.  
  18.     // Create a fetchXml statement
  19.     var fetchXml = "<fetch mapping='logical'> " +
  20.                    "     <entity name='t4a_matchingrule'> " +
  21.                    "         <attribute name='t4a_name'/> " +
  22.                    "         <filter type='and'> " +
  23.                    "             <condition attribute='t4a_commissiontype' operator='eq' value='" + commissionType + "'/> " +
  24.                    "         </filter> " +
  25.                    "     </entity> " +
  26.                    " </fetch>";
  27.  
  28.     // build Grid Layout
  29.     var layoutXml = "<grid name='resultset' " +
  30.                                "object='2' " +
  31.                                "jump='t4a_matchingrule' " +
  32.                                "select='1' " +
  33.                                "icon='1' " +
  34.                                "preview='1'>" +
  35.                             "<row name='result' " +
  36.                                 "id='t4a_matchingruleid'>" +
  37.                              "<cell name='t4a_name' " +
  38.                                    "width='200' />" +
  39.                             "</row>" +
  40.                          "</grid>";
  41.  
  42.     // add the Custom View to the indicated [lookupFieldName] Control
  43.     Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml,true);
  44. }

 

A more complex example shown here allows the lookup of relationships where we need to select client contacts from the selected agency

  1. var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  2.                "     <entity name='contact'> " +
  3.                "         <attribute name='accountid'/> " +
  4.                "         <attribute name='contactid'/> " +
  5.                "         <attribute name='fullname'/> " +
  6.                "         <link-entity name='account' from='accountid' to='parentcustomerid' alias='ClientAccount'> " +
  7.                "             <filter type='and'> " +
  8.                "                 <condition attribute='ts_isagent' operator='eq' value='0'/> " +
  9.                "             </filter> " +
  10.                "             <link-entity name='customerrelationship' from='customerid' to='accountid' alias='Customer'> " +
  11.                "                  <link-entity name='relationshiprole' from='relationshiproleid' to='customerroleid'> " +
  12.                "                    <filter type='and'> " +
  13.                "                        <condition attribute='name' operator='eq' value='Agency Client'/> " +
  14.                "                    </filter> " +
  15.                "                  </link-entity> " +
  16.                "                   <link-entity name='account' from='accountid' to='partnerid' alias='PartnerAccount'> " +
  17.                "                        <filter type='and'> " +
  18.                "                            <condition attribute='accountid' operator='eq' value='" + Xrm.Page.getAttribute("customerid").getValue()[0].id + "'/> " +
  19.                "                        </filter> " +
  20.                "                   </link-entity> " +
  21.                "               </link-entity> " +
  22.                "           </link-entity> " +
  23.                "       </entity> " +
  24.                "</fetch>";
  25.  
  26. // build Grid Layout
  27. var layoutXml = "<grid name='resultset' " +
  28.                            "object='2' " +
  29.                            "jump='contact' " +
  30.                            "select='1' " +
  31.                            "icon='1' " +
  32.                            "preview='1'>" +
  33.                        "<row name='result' " +
  34.                             "id='contactid'>" +
  35.                          "<cell name='fullname' " +
  36.                                "width='200' />" +
  37.                         "</row>" +
  38.                      "</grid>";

Finally, a BIG thank you to James Downey – the author of the FetchXml builder. I still use this tool in its CRM 4 livery, still loving it… a true rapid fetchxml building tool.

This entry was posted in CRM 2011 and tagged . Bookmark the permalink.

Comments are closed.