Dynamics crm(OP) 通过界面窗体上的字段筛选子网格列表数据

最近有一个新的需求,需要根据界面上的某个字段值筛选界面嵌入的子网格新增时的数据源,即数据源时根据界面上某个字段要求过滤后的。

 

实现操作如下:

需要通过按钮注册工具Ribbon WorkBench工具进入到需要设置功能的solution中,具体如下;

Dynamics crm(OP) 通过界面窗体上的字段筛选子网格列表数据_第1张图片

 地下的就是需要设置的一些displayrules和enablerules。

js脚本具体内容如下:

if (typeof (CSI) == "undefined") {
	CSI = {};
}

CSI.contactListFilter = function (context, selectedEntityTypeName, selectedControl, firstPrimaryItemId) {
	debugger;

	if (selectedControl == null || typeof (selectedControl) == undefined) return;

	//获取当前主实体名称
	var primaryEntityName = context.data.entity.getEntityName();
	//当前实体不是new_communicationplan且不是new_minutes,不做条件过滤
	if (primaryEntityName != "new_communicationplan" && primaryEntityName != "new_minutes") return;

	//准备lookupOptions属性
	var optionAttr = {
		fetchXml: "",
		//searchText: "",
		defaultViewId: "113CED8F-C9D3-EB11-A7A6-A6CCCBFF1FF9"
	};

	var customerType = context.getAttribute("new_customertype").getValue();
	if (customerType === 0) {
		//当客户类型为“华为客户”时,根据pdusdu值筛选联系人
		var pdusdu = context.getAttribute("new_pdusdu").getValue();
		if (pdusdu != null) {
			//optionAttr.searchText = pdusdu[0].name;
			optionAttr.fetchXml = "";
		}
	} else if (customerType === 1) {
		//当客户类型为“非华为客户”时,根据非华为客户值筛选联系人
		var nonHWCustomer = context.getAttribute("new_nonhwcustomer").getValue();
		if (nonHWCustomer != null) {
			//optionAttr.searchText = nonHWCustomer[0].name;
			optionAttr.fetchXml = "";
		}
	}

	//如果没选择客户,则筛选结果为空
	if (optionAttr.fetchXml == "") {
		optionAttr.fetchXml = "";
	}

	optionAttr.fetchXml = encodeURIComponent(optionAttr.fetchXml).replace(/’/g, "%22");

	CSI.filterAddExisting(selectedEntityTypeName, primaryEntityName, selectedControl, firstPrimaryItemId, optionAttr);
}

/**
 *  添加多对多关系记录
 *
 */

CSI.filterAddExisting = function (selectedEntityTypeName, primaryEntityName, selectedControl, firstPrimaryItemId, optionAttr) {
	debugger;
	//define data for lookupOptions
	var lookupOptions = {
		defaultEntityType: selectedEntityTypeName,
		entityTypes: [selectedEntityTypeName],
		allowMultiSelect: true,
		disableMru: true,
		defaultViewId: optionAttr.defaultViewId,
		//searchText: optionAttr.searchText,
		customFilters: [optionAttr.fetchXml],
		customFilterTypes: [selectedEntityTypeName]
		/*filters: [{
			filterXml: optionAttr.fetchXml,
			entityLogicalName: selectedEntityTypeName
		}]*/
	};

	// Get account records based on the lookup Options
	Xrm.Utility.lookupObjects(lookupOptions).then(
		function (success) {
			console.log(success);
			/**自定义过虑后选中记录标准功能无法创建关联记录,目前方案是手工创建 */
			Xrm.Utility.getEntityMetadata(primaryEntityName).then(function (primaryEntityData) {
				Xrm.Utility.getEntityMetadata(selectedEntityTypeName).then(function (relatedEntityData) {
					for (var i = 0; i < success.length; i++) {
						CSI.associateAddExistingResults(primaryEntityData, relatedEntityData, firstPrimaryItemId.replace("{", "").replace("}", ""), success[i].id.replace("{", "").replace("}", ""), selectedControl);
					}
					CSI.refreshAndClose(selectedControl);
				});
			});
		},
		function (error) {
			console.log(error);
		}
	);
}

CSI.associateAddExistingResults = function (primaryEntityData, relatedEntityData, firstPrimaryItemId, parentRecordId, selectedControl) {
	var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
	var req = new XMLHttpRequest();
	req.open("POST", clientUrl + "/api/data/v9.0/" + primaryEntityData.EntitySetName + "(" + firstPrimaryItemId + ")/" + selectedControl.getRelationship().name + "/$ref", false);
	req.setRequestHeader("Accept", "application/json");
	req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	req.setRequestHeader("OData-MaxVersion", "4.0");
	req.setRequestHeader("OData-Version", "4.0");
	req.onreadystatechange = function () {
		if (this.readyState == 4) {
			req.onreadystatechange = null;
			if (this.status == 204) {
				//Success - No Return Data - Do Something
			} else {
				var error = JSON.parse(this.response).error;
				//0x80040237 code means a matching key already exist
				if (error.code != "0x80040237") {
					Xrm.Utility.alertDialog(error.message);
				}
			}
		}
	};

	var reqContent = {
		"@odata.id": clientUrl + "/api/data/v9.0/" + relatedEntityData.EntitySetName + "(" + parentRecordId + ")"
	};
	req.send(JSON.stringify(reqContent));
}

CSI.refreshAndClose = function (selectedControl) {
	$("#lookupDialogCancelBtn").click();
	selectedControl.refresh();
}

最后,实现效果如下:

Dynamics crm(OP) 通过界面窗体上的字段筛选子网格列表数据_第2张图片

Dynamics crm(OP) 通过界面窗体上的字段筛选子网格列表数据_第3张图片 

 

你可能感兴趣的:(Dynamics,crm开发相关,javascript)