需求:页面上有一个字段是Account类型的Lookup,另一个字段是Contact类型的Lookup。要求打开Contact lookup时只显示Account的Primary Contact的过滤视图。
分析:要做到这个功能,首先要重写(增加)Contact lookup的“显示查找视图”的事件;然后,在页面加载的时候就要把这个事件添加到Contact lookup上,在Account的值发生变化时也要做相应的改变,比如Account的值被清空的情况下,我们的自定义过滤视图就应该显示全部的Contact,所以这时要再次改写“显示查找视图”的事件。
步骤:
(1)
创建一个Javascript文件,上传到Webresource,并添加到Form Libraries。
代码:
oArg = { items: null, customViews: null, setCustomView: function (viewId, objectType, viewDisplayName, fetchXml, layoutXml) { customView = { recordType: objectType, id: viewId, name: viewDisplayName, fetchXml: fetchXml, layoutXml: layoutXml }; this.customViews = new Array(); this.customViews[0] = customView; }, setLookupItems: function (ids, names, type) { var idArr = ids.split(';'); var nameArr = names.split(';'); this.items = new Array(); for (var i = 0; i < idArr.length; i++) { if (idArr[i].length >= 32) { this.items[i] = { oid: idArr[i], outerText: nameArr[i], oType: type, getAttribute: function (value) { switch (value) { case 'oid': { return this.oid; } case 'otype': { return this.oType; } default: { return null; } } } }; } } } };
(2)
创建打开过滤视图的Javascript,添加到Form Libraries:
代码:
//Open filterd lookup view of contact base on Account function OpenLookUpView() { var element = document.getElementById("contactid");//The contact lookup field id on the form var serverUrl = Xrm.Page.context.getServerUrl(); var client = Xrm.Page.getAttribute("customerid").getValue(); var viewId = "{00000000-0000-0000-0000-000000000001}"; // a dummy view ID var objectType = 2; //contact var viewDisplayName = 'Client contact'; if (client != null) { client = client[0].id; var fetchXml = "<fetch version='1.0' " + "output-format='xml-platform' " + "mapping='logical'>" + "<entity name='contact'>" + "<attribute name='fullname' />" + "<attribute name='telephone1' />" + "<attribute name='contactid' />" + "<attribute name='emailaddress1' />" + "<attribute name='address1_stateorprovince' />" + "<attribute name='address1_county' />" + "<attribute name='address1_city' />" + "<order attribute='fullname' " + "descending='false' />" + "<link-entity name='account' from='primarycontactid' to='contactid' alias='ab'>" + "<filter type='and'>" + "<condition attribute='accountid' " + "operator='eq' " + "value='" + client + "' />" + "</filter>" + "</link-entity>" + "</entity>" + "</fetch>"; var layoutXml = "<grid name='resultset' " + "object='1' " + "jump='name' " + "select='1' " + "icon='1' " + "preview='1'>" + "<row name='result' " + "id='contactid'>" + "<cell name='fullname' " + "width='100' />" + "<cell name='address1_city' " + "width='80' />" + "<cell name='address1_stateorprovince' " + "width='80' />" + "<cell name='address1_county' " + "width='80' />" + "<cell name='telephone1' " + "width='80' />" + "<cell name='emailaddress1' " + "width='100' />" + "</row>" + "</grid>"; oArg.setCustomView(viewId, objectType, viewDisplayName, fetchXml, layoutXml); //Overwrite the lookup event to show custom filtered view element.onshowdialog = function (event) { var result = window.showModalDialog(serverUrl + '/_controls/lookup/lookupinfo.aspx?AllowFilterOff=1&DefaultType=' + objectType + '&DisableQuickFind=0&DisableViewPicker=0&LookupStyle=single&ShowNewButton=1&objecttypes=' + objectType + '&ShowPropButton=1&browse=0&DefaultViewId=' + viewId, oArg, 'dialogHeight:630px;dialogWidth:600px;resizable:yes;'); event.oLookupItems = result; } } else { viewDisplayName = 'All contacts'; var fetchXml = "<fetch version='1.0' " + "output-format='xml-platform' " + "mapping='logical'>" + "<entity name='contact'>" + "<attribute name='fullname' />" + "<attribute name='telephone1' />" + "<attribute name='contactid' />" + "<attribute name='emailaddress1' />" + "<attribute name='address1_stateorprovince' />" + "<attribute name='address1_county' />" + "<attribute name='address1_city' />" + "<order attribute='fullname' " + "descending='false' />" + "</entity>" + "</fetch>"; var layoutXml = "<grid name='resultset' " + "object='1' " + "jump='name' " + "select='1' " + "icon='1' " + "preview='1'>" + "<row name='result' " + "id='contactid'>" + "<cell name='fullname' " + "width='100' />" + "<cell name='address1_city' " + "width='80' />" + "<cell name='address1_stateorprovince' " + "width='80' />" + "<cell name='address1_county' " + "width='80' />" + "<cell name='telephone1' " + "width='80' />" + "<cell name='emailaddress1' " + "width='100' />" + "</row>" + "</grid>"; oArg.setCustomView(viewId, objectType, viewDisplayName, fetchXml, layoutXml); //Overwrite the lookup event to show custom filtered view element.onshowdialog = function (event) { var result = window.showModalDialog(serverUrl + '/_controls/lookup/lookupinfo.aspx?AllowFilterOff=1&DefaultType=' + objectType + '&DisableQuickFind=0&DisableViewPicker=0&LookupStyle=single&ShowNewButton=1&objecttypes=' + objectType + '&ShowPropButton=1&browse=0&DefaultViewId=' + viewId, oArg, 'dialogHeight:630px;dialogWidth:600px;resizable:yes;'); event.oLookupItems = result; } } }
(3)
在Form onload和Account onchange的event添加刚才我们加进来的library,Function填“OpenLookUpView”
好的。就是这样。
补充:
如果遇到这个错误:You do not have sufficient privileges to open this Lookup dialog box. 请检查一下objecttypecode对了没有。
如果自定义视图没有生效,请检查第(1)步的Javascript有没有添加到页面库中。