How to perform multiple CRUD operations using executeMultiple Web API Request in Dynamics 365 CRM

By | July 16, 2019

Introduction

In this blog, we will learn how to perform multiple CRUD operations in a single Web API request using executeMultiple function. Currently, if we perform the CRUD operation on Dynamics 365 CRM then there is a need to fire the API request to each CRUD API operation. But with the help of executeMultiple we can execute multiple CRUD operations with just a single WebAPI request.

For example, let’s create some multiple Account entity records.

Please find below the source code to perform this function:

Create first account object for executing the create request.

var account1 = new Object();

account1[“name”] = “Account_1”; //account name

account1[“industrycode”] = 2; //industry code

var request1 = new Object();

request1.getMetadata = function () {

            return {

                boundParameter: undefined,

                operationType: 2,

                operationName: “Create”

          };

};

request1.etn = “account”; //entity set name

request1.payload = account1; //entity record data

 

Create second account object for executing the create request.

 

var account2 = new Object();

account2[“name”] = “Account_2”;

 account1[“industrycode”] = 3;

var request2 = new Object();

request2.getMetadata = function () {

            return {

                boundParameter: undefined,

                operationType: 2,

                operationName: “Create”

          };

};

request2.etn = “account”; //entity set name

request2.payload = account2; //entity record data

 

After creating the individual CRUD operation object push into array. So first create one array variable.

 

var batchReqCollection = [];

Add individual CRUD operation object into request collection.

//add first account request

batchReqCollection.push(request1);

//add second account request

batchReqCollection.push(request1);

 

After preparing the request collection, pass to the executeMultiple WebAPI function.

 

Xrm.WebApi.executeMultiple(batchReqCollection).then(

                   function (results) {   

                        //get success result after execute the function                  

                   },

                   function (error) {

                   });

 

The getMetadata() function description are as follows;

1. boundParameter: It specifies whether you perform CRUD operation, execute action or function. This parameter is specified with the following value:-

undefined: If you want to perform CRUD request.

Null: If execute action or function is not bounded to any entity.

EnityName: If execute action or function is not bounded to any entity.

2. OperationName: It specifies the Action name, Function name and any CRUD operation that you want to perform.

3. operationType: This parameter is specified with the following value:

0: If you want to execute Web API Action.

1: If you want to execute WebAPI Action Function.

2: If you want to perform CRUD operation.

Conclusion:

So using executeMultiple Web API function we can execute multiple Action, Function or CRUD operation in a single Web API request.