1

Poking around online it seems that odata deletes must occur one at a time, specifying the entity record's PK each time. But this SO post somewhat ambiguously hints at batch-style delete. If this is real, can someone give a working example?

The odata query that gets me the 3,200+ records I'd like to delete is this:

https://mycustomer.crm.dynamics.com/api/data/v9.1/mycustomentities?$filter=quoteid eq 43dfd5ef-ee4b-ed11-bba2-6045bd0054e1

Aside from obscuring the customer's name and initials in the URL above, it's real, and brings back a lot of records.

I have a use case in which I need to delete thousands of custom entity records via odata within Dynamics 365 for Customer Engagement (CRM). Doing this one at a time is SLOW. Is there any way to make it fast? I can query all the records I want to delete with a simple $filter, but would very much like to delete that same set in one shot.

Is there any way it can be done?

halfer
  • 19,824
  • 17
  • 99
  • 186
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158

1 Answers1

0

There are 2 freely available Addon

  1. CRM RestBuilder
  2. Dataverse RestBuilder

These are basically tools to create javascript webapi query and that is almost the same for Odata.

Now below code is for ajx odata query, intresting for us api call https://mycustomer.crm.dynamics.com/api/data/v9.1/BulkDelete

and the parameters passed to it. Take a look at docs soem of the parameters are optional and we do not worry about them. Interesting for us is queryset, which hold criteria for deleteting records.

    var parameters = {};
    var queryset1 = {
        EntityName: "account",
        ColumnSet: {
            AllColumns: true
        },
        Distinct: false,
    };

queryset1["@odata.type"] = "Microsoft.Dynamics.CRM.QueryExpression";
parameters.QuerySet = [queryset1];
    parameters.JobName = "";
    parameters.SendEmailNotification = true;
    var torecipients1 = {};
    torecipients1.activitypartyid = "00000000-0000-0000-0000-000000000000"; //Delete if creating new record 
    torecipients1["@odata.type"] = "Microsoft.Dynamics.CRM.activityparty";
    parameters.ToRecipients = [torecipients1];
    var ccrecipients1 = {};
    ccrecipients1.activitypartyid = "00000000-0000-0000-0000-000000000000"; //Delete if creating new record 
    ccrecipients1["@odata.type"] = "Microsoft.Dynamics.CRM.activityparty";
    parameters.CCRecipients = [ccrecipients1];
    parameters.RecurrencePattern = "";
    parameters.StartDateTime = new Date("10/18/2022").toISOString();
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v9.1/BulkDelete",
        data: JSON.stringify(parameters),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var results = data;
        },
        error: function(xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });

Also Take a look at this answer this will guide you as well

In addition are you restricted from your webaAPI only, do you by any chance are allowed to use XRMToolBox. If yes you are numerous opportunities to perform bulk delete. Easiest will be with sql statement.

AnkUser
  • 5,421
  • 2
  • 9
  • 25
  • Thanks AnkUser, but it seems like these are custom tools and/or custom UIs. I need something purely backend, using `DELETE` odata calls. My system is backend and must be run headlessly and programmatically without any user involvement. – HerrimanCoder Oct 18 '22 at 14:59
  • my point of above js code example was how the call is been made for api bulkdelete with parameters. Try postman call your dynamics api .. /api/data/v9.1/BulkDelete and pass json parameters as above, this should give your Odata call with it's parameter. In addition you can also you CRM SDK and use bulkdelete operation as well. – AnkUser Oct 18 '22 at 15:43
  • But I'm unclear what BulkDelete is on the end of that odata query. Is that supported natively? Or is it some custom thing that must be set up in the CRM environment? Can you show me a pure odata example that I could call with Postman? Preferably with js/ajax out of the picture because I cannot use that. – HerrimanCoder Oct 18 '22 at 16:00