Friday, November 15, 2019

Call Global Custom Action using JavaScript in Dynamics 365



Call Global Custom Action using JavaScript in Dynamics 365

Sometimes there are scenarios you need to create Global Actions where you don't specify an entity in particularly. When you create such global action and if you need to call that action from your JavaScript code or on Button Click. Here is the way to do that.

Example of calling Global Action Without Parameters:

Create a Global Action





























Add Steps and Copy Action Unique Name (My Custom Action)






















JavaScript to call Action

//Execute the created global action using Web API.
function CallGlobalCustomAction() {
    
    //get the current organization name
    var serverURL = Xrm.Page.context.getClientUrl();

    //query to send the request to the global Action 
    var actionName = "new_MyCustomAction"; // Global Action Unique Name

    //Pass the input parameters of action
    var data = {};

    //Create the HttpRequestObject to send WEB API Request 
    var req = new XMLHttpRequest();
    //Post the WEB API Request 
    req.open("POST", serverURL + "/api/data/v8.0/" + actionName, 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 || this.status == 204)
            {
                alert("Action Executed Successfully...");
               
            }
            else
            {
                var error = JSON.parse(this.response).error;
                alert("Error in Action: "+error.message);
            }
        }
    };
    //Execute request passing the input parameter of the action 
    req.send(window.JSON.stringify(data));
}




Example of calling Global Action with Parameters:























JavaScript to call Action

//Execute the created global action using Web API.
function CallGlobalCustomAction() {
    
    //get the current organization name
    var serverURL = Xrm.Page.context.getClientUrl();

    //query to send the request to the global Action 
    var actionName = "new_MyCustomAction"; // Global Action Unique Name

    //set the current loggedin userid in to _inputParameter of the 
    var InputParameterValue = Xrm.Page.context.getUserId();
 
    //Pass the input parameters of action
    var data = {
    "MyInputParameter": InputParameterValue
    };
//Create the HttpRequestObject to send WEB API Request var req = new XMLHttpRequest(); //Post the WEB API Request req.open("POST", serverURL + "/api/data/v8.0/" + actionName, 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 || this.status == 204) { alert("Action Executed Successfully...");

               //You can get the output parameter of the action with name as given below
               result = JSON.parse(this.response);
               alert(result.MyOutputParameter);
} else { var error = JSON.parse(this.response).error; alert("Error in Action: "+error.message); } } }; //Execute request passing the input parameter of the action req.send(window.JSON.stringify(data)); }

No comments:

Post a Comment