1

I'm trying to create a bulk delete job in Dynamics 365 using the web api. As reference I've used the following web pages:

I'm using api-version 9.1. I've gotten most of it to work and have removed quite a few validation errors, so I know I'm on the right track. However, now I get the following error message: "The Entity bookableresourcebooking does not support Synchronous Bulk Delete". When I try to create the same bulk delete job manually in Dynamics, no errors occur. Can anyone help me resolve this error? The relevant code I'm using:

      var relativeUrl = "BulkDelete()";
      var bulkDelete = new BulkDeleteRequest("Delete all future bookings");
      var querySet = new QuerySet();
      querySet.EntityName = "bookableresourcebooking";
      querySet.Distinct = false;
      var conditionStarttimeGreaterEqualToday = new Condition();
      conditionStarttimeGreaterEqualToday.AttributeName = "starttime";
      conditionStarttimeGreaterEqualToday.Operator = "OnOrAfter";
      conditionStarttimeGreaterEqualToday.Values = new List<ValueClass>();
      conditionStarttimeGreaterEqualToday.Values.Add(new ValueClass(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToUniversalTime().ToString("o"), "System.DateTime"));
      var conditionVoltooidOpEmpty = new Condition();
      conditionVoltooidOpEmpty.AttributeName = "new_voltooidop";
      conditionVoltooidOpEmpty.Operator = "Null";
      conditionVoltooidOpEmpty.Values = new List<ValueClass>();

      querySet.Criteria = new Criteria();
      querySet.Criteria.FilterOperator = "And";
      querySet.Criteria.Conditions.Add(conditionStarttimeGreaterEqualToday);
      querySet.Criteria.Conditions.Add(conditionVoltooidOpEmpty);

      bulkDelete.QuerySet.Add(querySet);
      await _crmClient.PostCRMData(relativeUrl, JsonConvert.SerializeObject(bulkDelete)); //Dependency injected httpclient.

Extra info:

  • bookableresourcebooking is a standard entity that comes with Field Service
  • new_voltooidop up is a custom datetime field I've added to this entity
Frank
  • 23
  • 8

1 Answers1

1

We have to make this job asynchronous and for that we have to pass RunNow: false. Then this error will disappear.

I have tested this working code in CRM REST Builder. But this is JS equivalent.

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

queryset1["@odata.type"] = "Microsoft.Dynamics.CRM.QueryExpression";
parameters.QuerySet = [queryset1];
parameters.JobName = "arun test";
parameters.SendEmailNotification = false;
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 = "FREQ=DAILY;";
parameters.StartDateTime = JSON.stringify(new Date("05/07/2021 13:30:00").toISOString());
parameters.RunNow = false;

var bulkDeleteRequest = {
    QuerySet: parameters.QuerySet,
    JobName: parameters.JobName,
    SendEmailNotification: parameters.SendEmailNotification,
    ToRecipients: parameters.ToRecipients,
    CCRecipients: parameters.CCRecipients,
    RecurrencePattern: parameters.RecurrencePattern,
    StartDateTime: parameters.StartDateTime,
    RunNow: parameters.RunNow,

    getMetadata: function() {
        return {
            boundParameter: null,
            parameterTypes: {
                "QuerySet": {
                    "typeName": "Collection(mscrm.QueryExpression)",
                    "structuralProperty": 4
                },
                "JobName": {
                    "typeName": "Edm.String",
                    "structuralProperty": 1
                },
                "SendEmailNotification": {
                    "typeName": "Edm.Boolean",
                    "structuralProperty": 1
                },
                "ToRecipients": {
                    "typeName": "Collection(mscrm.activityparty)",
                    "structuralProperty": 4
                },
                "CCRecipients": {
                    "typeName": "Collection(mscrm.activityparty)",
                    "structuralProperty": 4
                },
                "RecurrencePattern": {
                    "typeName": "Edm.String",
                    "structuralProperty": 1
                },
                "StartDateTime": {
                    "typeName": "Edm.DateTimeOffset",
                    "structuralProperty": 1
                },
                "RunNow": {
                    "typeName": "Edm.Boolean",
                    "structuralProperty": 1
                }
            },
            operationType: 0,
            operationName: "BulkDelete"
        };
    }
};

Xrm.WebApi.online.execute(bulkDeleteRequest).then(
    function success(result) {
        if (result.ok) {
            var results = JSON.parse(result.responseText);
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);