How to call Actions from Javascript Microsoft Dynamics

Muhammad Taqi picture Muhammad Taqi · Apr 14, 2019 · Viewed 10.7k times · Source

I have a requirement to call a Action Process from Javascript. My Action Accept 2 Input Parameters and 1 output Param. Below is the screenshot of my Action

enter image description here I have a textField in my Form, and on it's onChange event I'm calling this CallAction Method. Below is the JavaScript

function CallAction() {

        var actionName = "taqi_getPrice";
        var actionParameters = {
            "base": "USD",
            "TotalPrice": "200"
        };
        var actionResponse = activateCustomAction(actionName, actionParameters);
    }
    function activateCustomAction(actionName, actionParams) {

        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
        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);
                    alert('Success');
                } else {
                    alert('fail');
                    //Xrm.Utility.alertDialog(this.statusText);
                    console.log(this);
                }
            }
        };
        req.send(JSON.stringify(actionParams));
    }

When running this script I'm getting the following error in chrome console

POST https://techgulf.crm4.dynamics.com/api/data/v9.0/taqi_getPrice 404

Sometime it also says

Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers

Answer

AnkUser picture AnkUser · Apr 15, 2019

Well I created Exact same Action as you mentioned in your screenshot, Except Entity I used is Account. I used below code to fire Action and it did worked for me without any issue and returned the value as expected.

enter image description here

May be for Testing you could provide static Guid and see how you get the result.

var parameters = {};
parameters.base = "123";
parameters.TotalPrice = "222";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(DC86C293-CA4F-E911-A82F-000D3A385A1C)/Microsoft.Dynamics.CRM.crmp_TestAction2", false);
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);
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(parameters));