5

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

1

2 Answers 2

3

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));
Sign up to request clarification or add additional context in comments.

Comments

2

Change the below line

req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);

like this one below:

req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_cars(" + Id + ")/Microsoft.Dynamics.CRM.taqi_getPrice", false);

Basically we need to pass name of the Entity Set with id of the record followed by name of the action appended with Microsoft.Dynamics.CRM. In case of global action, we just need the Microsoft.Dynamics.CRM.<<ActionName>>.

Reference

Looks like you need a synchronous Action call execution (as you’re using false in req.open) otherwise you can use Xrm.WebApi.online.execute which is always Asynchronous. Read more

6 Comments

Thank you for your Reply. I changed the code as you suggested but I'm still getting 404. VM946:36 POST techgulf.crm4.dynamics.com/api/data/v9.0/… 404
@MuhammadTaqi make sure your entity name taqi_cars is correct. Recommend to use CRM REST Builder for composing queries, to avoid errors.
are you sure that the prefix: "Microsoft.Dynamics.CRM." should be present?
@Malachy may not be necessary am not sure, but CRM Rest Builder added it :)
Thanks everyone for your reply. Yes it was my mistake I was using wrong entity name. Everything is working fine now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.