javascript - 如何使用 Web Api 操作 AddSolutionComponent 通过 javascript 将实体添加到解决方案?

标签 javascript xmlhttprequest dynamics-crm dynamics-365 dynamics-crm-webapi

我想使用 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 完成工作吗?

最佳答案

一些变化:

  1. 评论了这一行associateEntityToSolution(newEntityId,entityUniqueName);,因为我猜这可能会进入循环。

  2. 在参数行中输入解决方案名称而不是解决方案 ID 'SolutionUniqueName':solutionUniqueName,

enter image description here

  • 更改了此行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/

    相关文章:

    javascript - 在 Firefox 上,CORS 请求给出错误 ":"(冒号)

    internet-explorer - 如何获取对嵌入在另一个应用程序中的 Internet Explorer 实例的 COM 引用

    javascript - 我有一个 div,它需要动态复制

    javascript - 为什么在使用严格时未定义匿名函数中的 "this"?

    javascript - 链接在 JQuery 的重复区域中不起作用

    javascript - 尝试对简单对象执行 JSON.parse 时出现 "Unexpected token"错误

    php - 我正在尝试将包含 XMHTTPRequest 脚本的 js 文件 src 到我的 HTML 中

    javascript - XHR 抛出无法捕获的错误

    javascript - 快速查看字段应通过 JavaScript 获取

    javascript - 在 MSCRM 2015 中使用 javascript 限制多实体查找