CRUD operations using executeMultiple in Dynamics 365 WebApi

Just another day at office and yet another challenge. Looked a simple one at the beginning. Basically here is the requirement

“Multiple create and update operations needs to be performed using Dynamics 365 Web API through client side.”

Off-course we can use the Xrm.WebApi.createRecord or Xrm.WebApi.updateRecord but in that case for as many records, those many server side calls need to be made from the client side.

But Microsoft has covered our back right! We have the executeMultiple method. So what’s the fuss about?

Well although we have the method but how to make it work. So basically the executeMultiple will accept an array of request object. And to create the request object, we can easily fall back to the documentation of execute method.

Unfortunately the document lacks in showing how to perform a CRUD operation request object. And that’s the fuss is all about. Things like this believe me, would take you days to resolve.

Well not to worry. If you think that this is going to take away your night’s sleep, I have done that for you. So let’s jump straight to the action here. Check for the parts highlighted in yellow. There in lies all the tricks!

And here is how to create request object for CREATE. Below is the code to create a request object for Account create.

var cAccount = {};
            cAccount["name"] = "CRUD Test";

// put your remaining fields to create the account object.

            var request = {};
            request.getMetadata = function () {
                return {
                     boundParameter: undefined,
                    operationType: 2,
                    operationName: "Create",
                     parameterTypes: {

                    }
                 };
            };

           request.etn = "account";
            request.payload = cAccount;

Below is the code to UPDATE an existing account.

var uAccount = {};
            uAccount["name"] = "CRUD Test – Update";

            var request2 = {};
            request2.getMetadata = function () {
                return {
                     boundParameter: undefined,
                    operationType: 2,
                    operationName: "Update",
                    parameterTypes: {

                    }
                };
            };

            request2.etn = "account";
            request2.payload = uAccount;

request2.id = “<put your account id here>”;

And finally the execute multiple code.

Xrm.WebApi.executeMultiple([request, request2]).then(
           function (results) {
               debugger;

               // parse your results here
            },
           function (error) {
           });

Hope this will help many and save your time!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)

Advertisements

One thought on “CRUD operations using executeMultiple in Dynamics 365 WebApi

  1. Have you found any way to replicate the ability to “ignore errors” like we can when using ExecuteMultiple in managed code? https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/execute-multiple-requests#specify-run-time-execution-options

    In my scenario I don’t want one failure to ruin the whole batch so I want to continue processing and then report individual failures separately. I’m not feeling very hopeful that this is possible in clientside though….

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