Fixed – Executing Action with Complex Output Parameters through Web API in Dynamics 365 v9.0

By | January 4, 2018

Introduction:       

While working on Dynamics 365, we get a chance to work on lot many things.

At times you work on C# or JavaScript or SSRS or SSIS. Having knowledge of so many languages, frameworks, or technologies could be taxing.

Sometimes we face errors while developing something and don’t have control over how it could be fixed.

One such thing was executing Action using Web API.

Prior to v9.0 update, if we tried executing Actions, having combination of complex and simple output parameters would result in errors like, Resource not found for the segment ‘ActionName’ or Request message has unresolved parameters.

In order to understand what complex and simple parameters are, please check a very well explained blog written by Andrew Butenko.

Now, in v9.0 we don’t have to worry about the combinations of the output parameters.

Irrespective of the combination of the output parameters, we are able to execute the Action.

Code Snippet:

function executeAction(accountId, actionName, clientUrl) {
    var functionName = "executeAction >>";
    var query = "";
    try {

        //Define the query to execute the action
        query = "accounts(" + accountId + ")/Microsoft.Dynamics.CRM."+actionName;

        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v9.0/" + query, true);
        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 /* complete */) {
                req.onreadystatechange = null;

                if (this.status == 200) {
                    //success callback
                    var result = JSON.parse(this.response);


                } else {
                    //error callback
                    var error = JSON.parse(this.response).error;
                }
            }
        };
        req.send(JSON.stringify(data));

    } catch (e) {
        throwError(functionName, e);
    }
}

Conclusion:

We can combine simple and complex output parameters and still execute the Action through Web API in Dynamics 365 v9.0

Export Dynamics CRM Reports

3 thoughts on “Fixed – Executing Action with Complex Output Parameters through Web API in Dynamics 365 v9.0

  1. Hayer

    Hi, Thanks for sharing. But I have got an error at line below:
    req.send(JSON.stringify(data));

    It is saying that “data” is not defined. Then I initialised the data like “var data = {};”

    but now I go following error at same link:
    Failed to load resource: the server responded with a status of 404 ()

    My custom action does not have any input parameter.

    Thanks

    1. inogic

      Hi,

      From your comment, it seems that you are not having any input parameters in your action. So you can simply change “req.send(JSON.stringify(data));” to “req.send();”

      Hope this helps.

      Thanks!

    2. Sohail

      function accountCustomAction() {
      var accountId = Xrm.Page.data.entity.getId();
      accountId = accountId.replace(“{“, “”).replace(“}”, “”);
      var parameters = {};
      parameters.PrimaryID = accountId;
      debugger;
      var req = new XMLHttpRequest();
      req.open(“POST”, Xrm.Page.context.getClientUrl() + “/api/data/v8.2/accounts(” + accountId + “)/Microsoft.Dynamics.CRM.cr1b8_CustomActionPluginAccountEntity”, 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 === 200) {
      var results = JSON.parse(this.response);
      if (results !== null) {
      if (results.AccountID !== null || results.AccountID !== “undefined”) {
      alert(“Success: ” + results.AccountID);
      }
      else {
      alert(“Action called successfully”);
      }
      }
      }
      else
      {
      alert(this.statusText);
      }
      }
      };
      req.send(JSON.stringify(parameters));
      }

Comments are closed.