{Dynamics CRM + Web API Actions} Execute action with an Entity as input parameter using Web API in Dynamics CRM 2016

“Executing action using Web API” – well this is a topic which has become banal. It’s been talked about, people are using it every now and then in their projects. After all 2016 has just swayed away the consultants and customers alike. In-fact I wrote a blog on the same topic almost 6 months back in January when CRM 2016 was in it’s early days.

https://debajmecrm.com/2016/01/06/invoke-your-custom-action-from-dynamics-crm-web-apidynamics-crm-2016/

Although this blog post was much appreciated and I cannot thank the readers more for this, I have getting repeated questions about multiple scenarios related to this and one of them is – How do I execute a global action using Web API with an entity as input parameter. Mostly in the examples shared on the blog, the input parameters are boolean and string.

So I decided to pen it down here. So let’s take a very simple example here.

For this demo, I have created an action named ‘Test Action’. That is a global action with Entity as the input parameter. The Entity type is account.

image

Now coming to the code. Below is the sample code to do the same.

function callAction() {

    var organizationUrl = Xrm.Page.context.getClientUrl();

    var account = {};
    account.name = "Test Account Name";
    account.description = "This account was created for action test.";
    account.revenue = 2000000.00; // decimal
    account.donotphone = true; //boolean field
    //account.logicalname = "account";
    var data = { "EntityArg": account };

    var query = "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) {
                var data = JSON.parse(this.response);

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

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

 

What the sample code is doing here? Well this extremely complex code is dynamically creating an account and then passing it. You can however retrieve an existing account and pass it as well. However here I am creating the account dynamically and passing it.

Simple piece of code. Hope this helps!

Advertisements

2 thoughts on “{Dynamics CRM + Web API Actions} Execute action with an Entity as input parameter using Web API in Dynamics CRM 2016

  1. The server response with error 400, I try create contact with this code:

    function callAction() {

    var organizationUrl = “https://[ORGNAME]crm4.dynamics.com/api/data/v9.1/abs_TestAction”

    var contact = new Object();
    contact.firstname = “JohnPRUENA”;
    contact.lastname = “Smith”;

    var data = { “EntityArg”: contact };

    var req = new XMLHttpRequest();
    req.open(“POST”,encodeURI(organizationUrl), 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”);
    var enviojson = JSON.stringify(data);
    req.onreadystatechange = function () {
    if (this.readyState == 4) {
    req.onreadystatechange = null;
    if (this.status == 200) {
    var data = JSON.parse(this.response);

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

    req.send(enviojson);
    }

    My action :

    https://ibb.co/sQGCDPg

    The action don’t have any step

    1. Hi Marc,
      Hope you have find the solution but just for the query.
      First I find you are using v9.1 endpoint, so I would rather suggest using Xrm.WebApi.execute method rather than the simple XmlHttpRequest.

      Also is you action global action or bound to an entity?

      -Debajit

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s