Execute Action with “EntityCollection” Parameter using Web API in Dynamics 365

By | December 20, 2016

Introduction

Recently, we had a requirement where we wanted to Execute Custom Action in Dynamics CRM by passing “EntityCollection” type parameter using Web API. We have discussed how to execute the action using Web API in one of our earlier blog. However, in this blog we just explained executing action with string parameter.

Here we are going to focus on executing action with “EntityCollection” parameter using Web API.

You can see below that we have created a Custom Action:

Dynamics CRM Web API

Below code snippet will help you to Execute Action using Web API:

function CallCustomActionWithEntityCollectionParameter() {
    try {

        var reqName = "new_Approve";
        var clientUrl = Xrm.Page.context.getClientUrl();
        var parameters = {
           "Accounts"://EntityCollection parameter
             [
                  {
                      "@odata.type": "Microsoft.Dynamics.CRM.account",//entity type
                      "accountid": "C4CA0B66-59B9-E611-8106-C4346BDC0E01",//Record's guid
                      "name": "Test",//name field of the account entity
                      "accountnumber": "123"//accountnumber field of the account entity
                  },
                  {
                      "@odata.type": "Microsoft.Dynamics.CRM.account",
                      "accountid": "CD67D78E-16BB-E611-8109-C4346BDC3C21",
                      "name": "Test2",
                      "accountnumber": "1234"
                  }
             ]
        };

        //Create request
        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v8.2/" + reqName, 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) {
                    //success callback   
                    console.log("Success");
                } else {
                    //error callback      
                    console.log("Error");
                }
            }
        };
        req.send(JSON.stringify(parameters));

    } catch (e) {
        alert(e.message);
    }
}

Conclusion:

This blog will help you to execute action by passing “EntityCollection” parameter using Web API in Dynamics 365.

70% of global 2000 companies apply gamification to improve productivity and returns!

Gamifics365 – Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!

2 thoughts on “Execute Action with “EntityCollection” Parameter using Web API in Dynamics 365

  1. kamrananjum

    HI Team,

    I want to pass entity reference on collection in each collection object. Can you help on this.
    For Eg: In above account object we have to pass contact also somewhere like
    {
    “@odata.type”: “Microsoft.Dynamics.CRM.account”,//entity type
    “accountid”: “C4CA0B66-59B9-E611-8106-C4346BDC0E01”,//Record’s guid
    “name”: “Test”,//name field of the account entity
    “accountnumber”: “123”//accountnumber field of the account entity
    contactid :/// this is reference field on account
    },
    So how to pass Entity reference in object. ??

    1. inogic

      Hi Kamran,

      You can pass entityreference field in the entitycollection parameter like in the below example:

      “Accounts”://EntityCollection parameter
      [
      {
      “@odata.type”: “Microsoft.Dynamics.CRM.account”,//entity type
      “accountid”: “9C1E6089-7739-E711-80EE-C4346BAC4304”,//Record’s guid
      “name”: “Test”,//name field of the account entity
      “accountnumber”: “123”,//accountnumber field of the account entity
      “primarycontactid”: { “@odata.type”: “Microsoft.Dynamics.CRM.contact”, “contactid”: “BC036ADB-F239-E711-80EE-C4346BAD526C” }
      },
      {
      “@odata.type”: “Microsoft.Dynamics.CRM.account”,
      “accountid”: “9C1E6089-7739-E711-80EE-C4346BAC4304”,
      “name”: “Test2”,
      “accountnumber”: “1234”,
      “primarycontactid”: { “@odata.type”: “Microsoft.Dynamics.CRM.contact”, “contactid”: “AC036ADB-F239-E711-80EE-C4346BAD526C” }
      }
      ]

      In the above example, we have set the “primarycontactid”(entityreference) field of Account entity.

      Hope this helps!

Comments are closed.