Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

CRM 2016 the best way to make bulk delete?

$
0
0

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, 


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>