Search code examples
javascriptxmlhttprequestdynamics-crmdynamics-365dynamics-crm-webapi

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


I want to add a custom entity to a custom solution in Dynamics CRM using javascript. I've done some research and it turns out that it probably can be be done with Actions. AddSolutionComponent should do the job, but I'm probably getting something wrong since I'm getting error 400 Request message has unresolved parameters.

The entity and the solution I am passing in the parameters are both created with javascript, and can found them both in the 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));
}

Am I missing something in the code? Are there any other solution to get the job done with javascript?


Solution

  • Couple of changes:

    1. Commented this line associateEntityToSolution(newEntityId,entityUniqueName); as I would guess this may go in loop.

    2. Put the solution name not the solution id in param line 'SolutionUniqueName':solutionUniqueName,

    enter image description here

    1. Changed this line req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true); to the correct Action web api call like this: 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));
    }
    

    I tested this in CRM REST Builder.