function addCityCustomView(viewId, entityName, viewDisplayName) { var oProvince = Xrm.Page.data.entity.attributes.get("sinochem_province"); var parentID = oProvince.getValue()[0].id; if (parentID != "") { var oCity = Xrm.Page.data.entity.attributes.get("sinochem_city"); var fetchXml = " <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + "<entity name='sinochem_nationinformation'>" + "<order attribute='sinochem_name' descending='false' />" + "<filter type='and'>" + "<condition attribute='sinochem_parent' operator='eq' value='" + parentID + "' />" + "<condition attribute='sinochem_level' operator='eq' value='3' />" + "</filter>" + "<attribute name='sinochem_name' />" + "<attribute name='sinochem_nationinformationid' />" + "</entity>" + "</fetch>" var layoutXml = "<grid name='resultset' object='10065' jump='sinochem_name' select='1' preview='1' icon='1'>" + "<row name='result' id='sinochem_nationinformationid'>" + "<cell name='sinochem_name' width='150' />" + "</row>" + "</grid>" var lookupControls = Xrm.Page.ui.controls.get("sinochem_city"); processLookup(lookupControls, viewId, entityName, viewDisplayName, fetchXml, layoutXml); }else { Xrm.Page.getControl("sinochem_city").setDisabled(true); // Disabled } } function processLookup(lookupControl, viewId, entityName, viewDisplayName, fetchXml, layoutXml) { // Get the default view of the lookup before it is modified var currentDefaultViewId = lookupControl.getDefaultView(); var oReq = getXMLHttpRequest(); if (oReq != null) { var url = "http://" + location.host + "/" + Xrm.Page.context.getOrgUniqueName() + "/XRMServices/2011/OrganizationData.svc/SavedQuerySet(guid'" + currentDefaultViewId + "')"; oReq.url = url; oReq.open("GET", url, true); oReq.onreadystatechange = function () { changeLookupDefaultView(oReq, lookupControl, viewId, entityName, viewDisplayName, fetchXml, layoutXml); }; oReq.send(); } } function isLookup(control, index) { return control.getControlType() == "lookup"; } function getXMLHttpRequest() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return null; } } } function changeLookupDefaultView(oReq, lookupControl, viewId, entityName, viewDisplayName, fetchXml, layoutXml) { if (oReq.readyState == 4) { if (oReq.status == 200) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.loadXML(oReq.responseText); var returnedTypeCodeNode = xmlDoc.selectSingleNode("//d:ReturnedTypeCode"); if (returnedTypeCodeNode) { var entityName = returnedTypeCodeNode.text; lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, false); lookupControl.setDefaultView(viewId); } } } }