我想使用 JavaScript 将自定义实体添加到 Dynamics CRM 中的自定义解决方案。
我做了一些研究,结果发现这可能可以通过操作来完成。
AddSolutionComponent
应该可以完成这项工作,但我可能遇到了一些问题,因为我收到错误 400 请求消息具有未解析的参数
。
我传入参数的实体和解决方案都是用javascript创建的,并且可以在crm中找到它们。
function associateEntityToSolution(entityId, solutionUniqueName, newSolutionId){
var param = {
'ComponentId': entityId , // newly created entity id
'ComponentType':1, // entity type
'SolutionUniqueName':solutionUniqueName, //newly created solution id
'AddRequiredComponents':false,
'IncludedComponentSettingsValues':null
};
var req = new XMLHttpRequest();
req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
associateEntityToSolution(newEntityId,entityUniqueName);
} else {
window.parent.opener.Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(param));
}
我在代码中遗漏了什么吗? 还有其他解决方案可以使用 javascript 完成工作吗?
最佳答案
一些变化:
评论了这一行
associateEntityToSolution(newEntityId,entityUniqueName);
,因为我猜这可能会进入循环。在参数行中输入解决方案名称而不是解决方案 ID
'SolutionUniqueName':solutionUniqueName,
- 更改了此行
req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true);
到正确的 Action Web api 调用,如下所示:req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v9.1/AddSolutionComponent", true);
-
function associateEntityToSolution(entityId, solutionUniqueName, newSolutionId){
var param = {
'ComponentId': entityId , // newly created entity id
'ComponentType':1, // entity type
'SolutionUniqueName':solutionUniqueName, // solution name (without spaces)
'AddRequiredComponents':false,
'IncludedComponentSettingsValues':null
};
var req = new XMLHttpRequest();
req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v9.1/AddSolutionComponent", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
//associateEntityToSolution(newEntityId,entityUniqueName);
} else {
window.parent.opener.Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(param));
}
我在 CRM REST Builder 中对此进行了测试。
关于javascript - 如何使用 Web Api 操作 AddSolutionComponent 通过 javascript 将实体添加到解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654055/