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)); }





Advertisement

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

28 thoughts on “Call Global Custom Action using JavaScript in Dynamics 365

  1. Thank your valuable content.we are very thankful to you.one of the recommended blog.which is very useful to new learners and professionals.content is very useful for hadoop learnersBest ASP.NET MVC Online Training InstituteBest Spring Online Training InstituteBest Devops Online Training Institute Best Datascience Online Training Institute Best Oracle Online Training Institute Best AWS Online Training Institute Best AngularJS Online Training Institute

    Like

  2. Great Article Cloud Computing Projects Networking Projects Final Year Projects for CSE JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    Like

  3. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

  4. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: