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.

image

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;
            alert(error.message);
        }
    }
};

req.send(window.JSON.stringify(data));

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.

image

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

image

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.

https://xrmtr11.crm5.dynamics.com/api/data/v8.0/new_TestAction

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.

Advertisements

Author: Debajit

I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

8 thoughts on “Invoke your Custom Action from Dynamics CRM Web API–Dynamics CRM 2016”

  1. Nice post deba .

    I was trying above example .I am getting below error

    “Request message has unresolved parameters” .Please suggest ?

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

    var query = “accounts(72AA82B2-FCB5-E511-80DD-6C3BE5A878BC)/Microsoft.Dynamics.Crm.new_MyAction”;
    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 == 204) {
    alert(“Action called successfully”);

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

    req.send(window.JSON.stringify(data));

    1. Hi Yusuf,
      That’s my bad. Here URL requests are case sensitive. In the blog I have mistakenly written Crm instead of CRM.
      Microsoft.Dynamics.Crm.new_MyAction should be Microsoft.Dynamics.CRM.new_MyAction
      try this and it should work.

  2. Thanks !! Deba,

    Its working fine.There is one more typo above.

    req.open(“POSTganizationUrl + “/api/data/v8.0/” + query, true);

    Your posts are very helpful.

  3. Hello,

    Do I understand correctly that this is in context with a specific account record based on? Please help me understand how to modify this to work in context with the currently viewed account record.

    Thanks,
    Mike

    1. Hi Mike,

      The action that I used in this demo is tied to an account entity. So it always needs to be called in context of an account record.

      When you open a account, in the form script you could use the guid of the account record that is open. All you need to do is, change the below from hardcoded value to dynamic value

      var query = “accounts(DE57510E-59A3-E511-80E4-3863BB35AD90)/Microsoft.Dynamics.CRM.new_TestAction”;

      to

      var id = Xrm.Page.data.entity.getId();
      var query = “accounts(” + id.replace(“}”, “”).replace(“{“, “”) + “)/Microsoft.Dynamics.CRM.new_TestAction”;

  4. Really like the example provided and this is the first site that included the global action URL. Does Microsoft have this documented somewhere? Also, I’m going to need to use an Entity input parameter and I’m curios if you could provide an example for this.

    Thanks!

    1. When I wrote this article it was early crm 2016 days and I had to literally trial and error to find out how it works. However SDK is lot more updated and you should have this documented in SDK by now.

      regarding the entity parameter, I will try this for myself and get back to you on this.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s