0
votes

I'm having trouble calling my global custom action using JavaScript in Dynamics CRM.

In CRM I created this global action that takes an in parameter and returns an out parameter. I've confirmed that the action is activated and works. The problem arises when I try to call it using JavaScript.

My JavaScript code is as follows:

callCustomAction: function (actionName, actionParameters) {
    var result = null;

    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + actionName), false);
    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) {
            req.onreadystatechange = null;

            if (this.status == 200) {
                result = JSON.parse(this.response);
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send(window.JSON.stringify(actionParameters));

    return result;
}

Now, to me this looks correct but I got an issue with the URL which at the moment leads to nowhere. Everywhere I look the URL for a global action is simply the organization URL followed by "/api/data/v8.2/" and the name of my action but for me it's not working and I can't figure out why.

Resolved The issue was using a mix of EntityReferences and strings as out parameters which made my action not show up under metadata

1
Do you have any specific error? You are very unclear in your question what exactly is the problem. "URL leading to nowhere" is not a problem - this is a POST action so you will not get any response from GET by posting the link in your browser... What error is brought back from the server when you execute your JS?Pawel Gradecki
When I do alert(this.status); I get 404 if that's what you mean. That's why I figured my URL is wrong.ponlu
And when you export your metadata (Customizations -> Developer Resources -> Download Odata Metadata) is your custom action available there and Input/Output parameters are ok? Also - are you sure this is not cut off by some cross site scripting policy, maybe you connect to your CRM via IP and call API via full domain name (I saw this error made too many times...)Pawel Gradecki
Also doing alert(this.status) is not really debugging. Attach some Developer Tools debugger from whatever browser you use and check the full error message (even without debugger you should be able to see it in Networking tab of your Developer Tools)Pawel Gradecki
I downloaded Odata Metadata but couldn't find my action. Could that be what's causing the issue? Don't know about cross site scripting policies.ponlu

1 Answers

0
votes

Global actions which have an output parameter associated cant be called using the Web API. This is not the case for actions having no output parameter.

This is because sometimes the status code is 200 and sometimes different. We noticed this bug and observed it to happen only with actions having output parameter. Also we noticed this happening on certain times and not frequently as in at times you will get output but sometimes not.

As a workaround, you need to use the conventional SOAP query and execute the workflow and then parse the response.

Hope MS solves this soon