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

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

 

实现操作如下:

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

 地下的就是需要设置的一些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 = "<filter type='and'><condition attribute='new_pdusdu' operator='eq' value='" + pdusdu[0].id + "' /></filter>";
		}
	} else if (customerType === 1) {
		//当客户类型为“非华为客户”时,根据非华为客户值筛选联系人
		var nonHWCustomer = context.getAttribute("new_nonhwcustomer").getValue();
		if (nonHWCustomer != null) {
			//optionAttr.searchText = nonHWCustomer[0].name;
			optionAttr.fetchXml = "<filter type='and'><condition attribute='parentcustomerid' operator='eq' value='" + nonHWCustomer[0].id + "' /></filter>";
		}
	}

	//如果没选择客户,则筛选结果为空
	if (optionAttr.fetchXml == "") {
		optionAttr.fetchXml = "<filter type='and'><condition attribute='statecode' operator='eq' value='999' /></filter>";
	}

	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();
}

最后,实现效果如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值