Hello,
I need to schedule a custom workflow activity that delete all entity records and populate it again from external source.
I am going to use bulk delete but as far as I know it is executed asynchronously (an delete records in a batches) by the CRM, so it is not easy to understand when it is completed (in order to be started the population process). I made some research in google and found the following solution:
private void DeleteAllRecords(IOrganizationService _service) { BulkDeleteRequest request = new BulkDeleteRequest { JobName = "Delete All", ToRecipients = new Guid[] { }, CCRecipients = new Guid[] { }, RecurrencePattern = string.Empty, QuerySet = new QueryExpression[] { new QueryExpression { EntityName = "my_entity" } } }; BulkDeleteResponse response = (BulkDeleteResponse)_service.Execute(request); Guid jobId = response.JobId; deleteInProcess = true; while (deleteInProcess) { Thread.Sleep(5000); QueryExpression query = new QueryExpression { EntityName = "bulkdeleteoperation" }; query.Criteria.AddCondition("asyncoperationid", ConditionOperator.Equal, jobId); query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 3); query.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 30); EntityCollection results = _service.RetrieveMultiple(query); if (results.Entities.Count > 0) { deleteInProcess = false; } } }
Is there a better solution?
Regards,