cancel
Showing results for 
Search instead for 
Did you mean: 
rampprakash

How to use Execute Multiple Web Api in JavaScript

Implementation Steps:

 

Consider a Scenario, You want to Create a Record and Update a Record in the Dataverse Environment. Initially if we want to perform such kind of operation we need to Have Multiple Calls, one for Create and Another For Update.

 

Syntax:

 

 

var requests = [req1, req2, req3];
Xrm.WebApi.online.executeMultiple(requests).then(successCallback, errorCallback);

 

 

 

Irrespective of entities we can trigger this Execute Multiple Operation, merging custom or standard entities also possible

 

Lets have an Quick Example:

 

I need to trigger Account Creation and Opportunity Updation in a Single call

 

Account Creation:

 

Declare a Global Variable:

 

 

var batchReqCollection = [];

 

 

Create Operation For Account:

 

 

var createAccount = new Object();
createAccount["accountName"] = "Execute Multiple Account" //accountName --> Field Name of Account

// Create a Request
var request = new Object();
request.getMetadata = function () {
return {
boundParameter: undefined,
operationType: 2,
operationName: "Create" // Operation
};
};
request.etn = "account"; //entity logicalName
request.payload = createAccount;

// Add the Request to Array
batchReqCollection.push(request);

 

 

Update Opportunity:

 

 

var updateOpportunity = new Object();
updateOpportunity["opportunityname"] = "Execute Multiple Opportunity Update" //opportunityname --> Field Name of Opportunity

// Create a Request
var request = new Object();
request.getMetadata = function () {
return {
boundParameter: undefined,
operationType: 2,
operationName: "Update" // Operation
};
};
request.etn = "opportunity"; //entity logicalName
request.payload = updateOpportunity;
request.id = opportunityID; // Pass GUID of Opportunity (it is used to Update a Record

// Add the Request to Array
batchReqCollection.push(request);

 

 

Once Both Create and Update Operation Triggered. 

 

 

Xrm.WebApi.executeMultiple(batchReqCollection).then(
            function (results) {
                //debugger; // Success Result
            },
            function (error) {
                //debugger; // Failure Result
            });

 

 

 

Sample Screenshot:

 

rampprakash_0-1643393533543.png

 

Based on this Screenshot, we can expand result and get the Status

 

Here Status is 204 and ok is True. Which denotes its a Success Result..

 

Happy Coding 🙂

Comments