Wednesday, 6 June 2012

Contact Filtered Lookup based on Party List(Multi select lookup)

Create filtered lookup based on records selected in party list. We have requirement to create filtered contact lookup to selected accounts(associated child contacts) in a party list. This is quite simple like filter lookup in MS CRM 2011 but only different thing need to do is collect all GUID’s(accounts) from party list and then create FetchXML query to retrieve entire child contacts based on the all collected GUID’s(accounts) from party list.

Read party list using JavaScript and store all GUID's into Array object :-
   
var a = 0;
  var Guids = new Array();
  if (Xrm.Page.getAttribute("customers").getValue() != null) {
        var _List = Xrm.Page.getAttribute("customers").getValue();
        if (_List.length > 0) {
            for (a = 0; a < _List.length; a++) {
                Guids[a] = _List[a].id;
            }
        }
    }

Create FetchXML query to retrieve entire child contacts  :-

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                   "<entity name='contact'>" +
                   "<attribute name='fullname' />" +
                   "<attribute name='contactid' />" +
                   "<order attribute='fullname' descending='false' />" +
                   "<link-entity name='account' from='accountid' to='parentcustomerid' alias='aa'>" +
                   "<filter type='or'>";
    for (var a = 0; a < _List.length; a++) {
        fetchXml += "<condition attribute='accountid' operator='eq' value='" + _List[a] + "'/>";
    }
    fetchXml += "</filter></link-entity></entity></fetch>";

Complete code:- 

function FilterLookup( _DisplayName, _FieldName, _Guids) {
    var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}";
    var entityName = "contact";
    var viewDisplayName = "Contacts for " + _DisplayName;
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                   "<entity name='contact'>" +
                   "<attribute name='fullname' />" +
                   "<attribute name='contactid' />" +
                   "<order attribute='fullname' descending='false' />" +
                   "<link-entity name='account' from='accountid' to='parentcustomerid' alias='aa'>" +
                   "<filter type='or'>";
    for (var a = 0; a < _List.length; a++) {
        fetchXml += "<condition attribute='accountid' operator='eq' value='" + _Guids[a] + "'/>";
    }
    fetchXml += "</filter></link-entity></entity></fetch>";

    //build grid layout
    var layoutXml = "<grid name='resultset' " +

                             "object='1' " +

                             "jump='fullname' " +

                             "select='1' " +

                             "icon='1' " +

                             "preview='1'>" +

                             "<row name='result' " +

                             "id='contactid'>" +

                             "<cell name='fullname' " +

                             "width='300' />" +

                         "</row>" +

                       "</grid>";


    //add new view view
 Xrm.Page.getControl(_FieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}


No comments:

Post a Comment