Tuesday, 21 March 2017

How to call a Global action from JavaScript?

We have covered how to call a custom action in JavaScript in a previous blog.
But sometimes there are scenarios you need to create Global Actions where you don't specify an entity in particularly. When you create such a global action and if you need to call that action from your JavaScript code how can you do it?

Let's say this is your Global Action. Please note the name and the Unique Name. Unique name is the one you need to use in the code.



















Then use the following code to call your action. Replace the "your action name" with the unique name of your action. 

function CallAction()
{
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_TestGlobalAction", 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 === 204) {
                alert("Action Called");
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

Make sure your action is activated!

No comments:

Post a Comment

How to assign a record to the current user via JavaScript

Recently I got a requirement from the customer to create a ribbon button on a custom entity so that a user can click on that button and get...