Tag Archives: dynamics crm web api action execute

Invoke your Custom Action from Dynamics CRM Web API–Dynamics CRM 2016

Continuing with my series of blog posts related to the wonderful Web API of CRM 2016, in this blog post, I will explain how to execute custom actions through Web API. No more use of creating complex SOAP queries from the client side to invoke your action. The wave of Web API is here to sweep off the old established complex rules.

Let’s create a custom action here. I name it TestAction for the account entity. Below is the screenshot for the same.


As you can see the, the Action is pretty simple. I have two input parameters for the action – ‘Subject’ and ‘Description’. All the action does is send an email with the Subject and Description populated from the input arguments of the action. I activate the action. Now let’s get dirty with the code to invoke this action from the client side.

var organizationUrl = Xrm.Page.context.getClientUrl();
var data = {
    "Description": "Test description",
    "Subject": "Invoking from Web API"

var query = "accounts(DE57510E-59A3-E511-80E4-3863BB35AD90)/Microsoft.Dynamics.CRM.new_TestAction";
var req = new XMLHttpRequest();
req.open("POST", organizationUrl + "/api/data/v8.0/" + query, 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) {
        req.onreadystatechange = null;
        if (this.status == 200) {
            alert("Action called successfully");

        } else {
            var error = JSON.parse(this.response).error;


Let me explain the code here. Since the action is based on the account entity, I can invoke it only from the context of an account record. DE57510E-59A3-E511-80E4-3863BB35AD90 is the guid of the account record. Check carefully here. I am invoking my action using the fully qualified namespace of my action message – Microsoft.Dynamics.CRM.new_TestAction

Like I do all the time to test my code, this time also I open CRM in my browser and pull up the powerful developer tool’s console.


I press enter and CRM does the magic! I check my emails and this is what I get.


Never mind the two records created here. I was so excited that I ran the code twice. Smile

Similar to this, you can call your global actions also which are not bound to entity. Below would have been the request URL if new_TestAction would have been a global action.


Please note you do not need to use the full qualified namespace for calling a global action unlike the action bound to entity.

Hope this helps! Till you read one of my blog posts, happy exploring CRM 2016.