簡體   English   中英

如何使用Web Api操作AddSolutionComponent使用javascript將實體添加到解決方案中?

[英]How to add an entity to a solution with javascript using Web Api action AddSolutionComponent?

我想使用JavaScript將自定義實體添加到Dynamics CRM中的自定義解決方案中。 我已經進行了一些研究,結果發現可以使用Actions來完成。 AddSolutionComponent應該可以完成這項工作,但是由於出現錯誤400 Request message has unresolved parameters ,我可能會出錯。

我要傳遞參數的實體和解決方案都使用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,

在此處輸入圖片說明

  1. 更改了此行req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true); 到正確的Action網絡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中對此進行了測試。

網址:

POST [Your Org]/api/data/v9.0/AddSolutionComponent

身體:

{
    "ComponentId" : "YourComponentGuid",
    "ComponentType" : "YourComponentType", 
    "SolutionUniqueName" : "YourSolutionUniqueName",
    "AddRequiredComponents" : "false", //false or true
    "DoNotIncludeSubcomponents" : "true" //false or true
}

可以通過對[Your Org]/api/data/v9.0/EntityDefinitions(LogicalName='YourEntityLogicalName')?$select=MetadataId進行GET請求來檢索ComponentId。

在此處搜索可用的組件類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM