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.
- function setMatchRuleLookup(lookupFieldName, resetSelection) {
- if (resetSelection == true) {
- // reset old selection for Matching rull
- Xrm.Page.getAttribute(lookupFieldName).setValue(null);
- }
- // Get the commission type – optionsetvalue
- var commissionType = Xrm.Page.getAttribute("t4a_commissiontype").getValue();
- // use randomly generated GUID Id for our new view
- var viewId = "{1DFB2B35-B07C-55D1-867D-258DEEBB87E2}";
- var entityName = "t4a_matchingrule";
- // give the custom view a name
- var viewDisplayName = "Matching rules for this commmission type";
- // Create a fetchXml statement
- var fetchXml = "<fetch mapping='logical'> " +
- " <entity name='t4a_matchingrule'> " +
- " <attribute name='t4a_name'/> " +
- " <filter type='and'> " +
- " <condition attribute='t4a_commissiontype' operator='eq' value='" + commissionType + "'/> " +
- " </filter> " +
- " </entity> " +
- " </fetch>";
- // build Grid Layout
- var layoutXml = "<grid name='resultset' " +
- "object='2' " +
- "jump='t4a_matchingrule' " +
- "select='1' " +
- "icon='1' " +
- "preview='1'>" +
- "<row name='result' " +
- "id='t4a_matchingruleid'>" +
- "<cell name='t4a_name' " +
- "width='200' />" +
- "</row>" +
- "</grid>";
- // add the Custom View to the indicated [lookupFieldName] Control
- Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml,true);
- }
A more complex example shown here allows the lookup of relationships where we need to select client contacts from the selected agency
- var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
- " <entity name='contact'> " +
- " <attribute name='accountid'/> " +
- " <attribute name='contactid'/> " +
- " <attribute name='fullname'/> " +
- " <link-entity name='account' from='accountid' to='parentcustomerid' alias='ClientAccount'> " +
- " <filter type='and'> " +
- " <condition attribute='ts_isagent' operator='eq' value='0'/> " +
- " </filter> " +
- " <link-entity name='customerrelationship' from='customerid' to='accountid' alias='Customer'> " +
- " <link-entity name='relationshiprole' from='relationshiproleid' to='customerroleid'> " +
- " <filter type='and'> " +
- " <condition attribute='name' operator='eq' value='Agency Client'/> " +
- " </filter> " +
- " </link-entity> " +
- " <link-entity name='account' from='accountid' to='partnerid' alias='PartnerAccount'> " +
- " <filter type='and'> " +
- " <condition attribute='accountid' operator='eq' value='" + Xrm.Page.getAttribute("customerid").getValue()[0].id + "'/> " +
- " </filter> " +
- " </link-entity> " +
- " </link-entity> " +
- " </link-entity> " +
- " </entity> " +
- "</fetch>";
- // build Grid Layout
- var layoutXml = "<grid name='resultset' " +
- "object='2' " +
- "jump='contact' " +
- "select='1' " +
- "icon='1' " +
- "preview='1'>" +
- "<row name='result' " +
- "id='contactid'>" +
- "<cell name='fullname' " +
- "width='200' />" +
- "</row>" +
- "</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.