Dynamics 365 N:N子网格添加现有Inline Lookup筛选
Posted Vic.Tang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamics 365 N:N子网格添加现有Inline Lookup筛选相关的知识,希望对你有一定的参考价值。
在N:N的子网格中添加现有记录,弹出的就是如下界面,而这个lookup的默认搜索视图是实体的查找视图,如果我只想显示筛选后的记录,那只需要配置搜索视图的filter条件。
但有些场景是没法通过filter实现的,比如这个lookup视图会用在很多场景,如果被被子网格的场景使用了,那其他的页面等的应用场景就不行了,另一种场景是我需要通过子网格父页面的某个字段的值去动态的筛选视图也是实现不了的。
对于默认的子网格没有开放任何的API去操作这个inline的lookup,在之前的版本网上会有很多帖子通过操作dom这种不被支持的方式也能实现,但现在有了一个新的API叫做Xrm.Utility.lookupObjects,这个API的具体说明可以见Docs,我们要做的就是自定义"添加现有记录"按钮的command事件,用我们自己的JS代替,代码如下,我们要的就是代码中的filters,其他的参数的意义这里就不说了,见API说明吧
var parentId = primaryControl.data.entity.getId().replace("{", "").replace("}", "");
var lookupOptions = {
allowMultiSelect: true,
defaultEntityType: this.Constants.Entities.ClassicSolution,
entityTypes: [this.Constants.Entities.ClassicSolution],
disableMru: true,
showNew: false,
searchText: "\\n", // Search by default
filters: [{ filterXml: "<filter type='and'><condition attribute='statuscode' operator='eq' value='2' /></filter>", entityLogicalName: this.Constants.Entities.ClassicSolution }],
viewIds: ['07A85D66-653C-EC11-9C13-A40C736E61F3']
};
var that = this;
Xrm.Utility.lookupObjects(lookupOptions).then(function (results) {
if (results.length > 0) {
BIDR.OM.Utilities.associateAddExistingResults(that.Constants.Entities.ProjectClassicSolution,
that.Constants.Entities.ProjectCollection, that.Constants.Entities.ClassicSolutionCollection,
that.Constants.Entities.ClassicSolution, parentId, gridControl, results);
}
}, function (error) { console.log(error) });
虽然上述JS能自定义了添加现有现有记录的的lookup弹窗,但也就破坏了原来的选择记录后确定后的逻辑,我们需要自定义lookup中选择记录并点击确定后的回调函数,同当前的父窗体记录进行关联,回调的代码如下
associateAddExistingResults: function (relationshipName, primaryEntitySetName, relatedEntitySetName, relatedEntity, parentRecordId, gridControl, results) {
/**
* 用于将过滤出的记录关联到父级实体上
* */
results.forEach(function (item) {
var lookupId = item.id.replace("{", "").replace("}", "");
var lookupEntity = item.entityType || results[index].typename;
var primaryId = parentRecordId;
var relatedId = lookupId;
if (lookupEntity.toLowerCase() != relatedEntity.toLowerCase()) {
// If the related entity is different to the lookup entity flip the primary and related id's
primaryId = lookupId;
relatedId = parentRecordId;
}
var associationData = { '@odata.id': BIDR.OM.Utilities.GetClientUrl() + "/api/data/v9.0/" + relatedEntitySetName + "(" + relatedId + ")" };
BIDR.OM.Utilities.AssociateRecord(primaryEntitySetName, primaryId, relationshipName, associationData);
if (gridControl) { gridControl.refresh(); }
});
}
将选中的子记录和父记录associate的代码,associate的API的body貌似不能多条,所以当你的子记录是多条时只能循环associate,当然你也可以patch批处理,我这边就偷懒了循环下就好了
AssociateRecord: function (lEntityName, lEntityId, relationshipName, associationData) {
var req = new XMLHttpRequest();
req.open("POST", this.GetClientUrl() + "/api/data/v9.0/" + lEntityName + "(" + lEntityId + ")/" + relationshipName + "/$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 || this.status === 1223) {
// Success
// Process the next item in the list
}
else {
// Error
var error = JSON.parse(this.response).error.message;
Xrm.Navigation.openAlertDialog(error);
}
}
};
req.send(JSON.stringify(associationData));
},
自此就实现了子网格上的添加现有记录后lookup视图的筛选了。
以上是关于Dynamics 365 N:N子网格添加现有Inline Lookup筛选的主要内容,如果未能解决你的问题,请参考以下文章
Dynamics 365Online Lookup查找字段多选
Get formContext inside Webresource in Dynamics 365
Dynamics 365Online Lookup查找字段多选
Dynamics 365Online Lookup查找字段多选
在 Microsoft Dynamics 365 Online中如何调试Plugins in
Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface