Apurva Yadav
Apurva Yadav

Reputation: 65

I want to delete multiple CRM records from Dynamics with Xrm.WebApi.online.executeMultiple

Multiple entity records have to be deleted in one call instead of multiple callbacks so trying to use Xrm.WebApi.online.executeMultiple to delete records. but the code written below is not working. Any help will be appreciated.

            for (var i=0; i<Checkbox.length; i++)
            {
                if(Checkbox[i].checked)
                {
                    var id = Checkbox[i].value;// GUID of the record to be deleted
                    Checkbox[i].checked = false;

                    DeleteRequests[i]={};
                    DeleteRequests[i].getMetadata = function(){
                        return{
                            boundParameter: undefined,
                            operationType: 2,
                            operationName: "Delete",
                            parameterTypes: {
                            }
                        }

                    }
                    DeleteRequests[i].etn="cme_entity";
                    DeleteRequests[i].payload=id;
                }
            }
            window.parent.Xrm.WebApi.online.executeMultiple(DeleteRequests).then(
                function (results) {alert("Success");},
                function (error) {alert("Failed");});

Getting weird error that this operation could not be processed. Please contact Microsoft.

Upvotes: 3

Views: 2728

Answers (1)

Ryan James
Ryan James

Reputation: 339

The issue has to do with how you are constructing the delete request objects. You need to declare a function that sets up the getMetadata function and the required entityReference object.

I've tested the below solution and it works.

var Sdk = window.Sdk || {};

Sdk.DeleteRequest = function (entityReference) {
    this.entityReference = entityReference;
    this.getMetadata = function () {
        return {
            boundParameter: null,
            parameterTypes: {},
            operationType: 2,
            operationName: "Delete",
        };
    };
};

for (var i = 0; i < Checkbox.length; i++) {
    if (Checkbox[i].checked) {
        var id = Checkbox[i].value;
        Checkbox[i].checked = false;

        DeleteRequests[i] = new Sdk.DeleteRequest({ entityType: "account", id: id }); 
    }
}

window.parent.Xrm.WebApi.online.executeMultiple(DeleteRequests).then(
    function (results) { alert("Success"); },
    function (error) { alert("Failed"); });

Unfortunately CRUD operations with Xrm.WebApi.online.execute and Xrm.WebApi.online.executeMultiple are not very well documented. I've written a blog post with some code samples.

The important parts are the declaration of the Sdk.DeleteRequest function as a property on window and instantiating a request object using new Sdk.DeleteRequest(). I experimented a little and determined that just simply creating a request object like you were doing before, even with the right attributes does not work either.

Hope this helps! :)

Upvotes: 2

Related Questions