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,
*This post is locked for comments
I didn't manage to figure out how to use paging with ExecuteMultipleRequest.
Hi,
The idea is good , but may I know how many data are you are going to delete in one request ?,
you can try with execute multiple request , seems this is faster than bulkdeleterequest.
If your code is populating the data, you could do the deletion from your code, no need to use bulk deletion job.
You could use the execute multiple to batch the deletes in your code
public static void BulkDelete(IOrganizationService service, DataCollection<EntityReference> entityReferences)
{
var multipleRequest = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};
// Add a DeleteRequest for each entity to the request collection.
foreach (var entityRef in entityReferences)
{
DeleteRequest deleteRequest = new DeleteRequest { Target = entityRef };
multipleRequest.Requests.Add(deleteRequest);
}
// Execute all the requests in the request collection using a single web method call.
ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
}
If you have more than 5k records, you will have to use paging when you retrieve records
Refer to this link to see how you can do paging - pull 5k records from CRM at a time and then use the BulkDelete method mentioned above to delete those 5k and then carry on doing until the last record
Just wanted to add this as well : What happens if you create a workflow on Email Created/Received and check if its bulk delete completed email, then start inserting your data if you do not want to use your code to do the bulk delete, this is just an idea and its possible.
Btw, I assume is this bulk delete is related to your question on other thread about external lookup values storing in CRM
Hi Kokulan,
Thanks for your help.
Your solution looks fine.
Most probably I do not understand something.
I need to start an entity population once the deletion is completed, that's why the emails sending is not the case.
Ho can I achieve that?
p.s. The solution will be running outside the working hours.
Regards,
Hi
The code in your question does not do anything special, all it does is submits a bulk delete job and then checks every 5 seconds if the job is completed.
But I think its an inefficient way of doing a bulk delete and getting the status of the job.
Your plugin whether it's run sync or async, is going to affect the performance of the system - so you will have to run your above code out of office hours
You do not have to do the above, and just receive an email once the deletion completed. Please see the following
Steps in Creating Bulk Delete Job
From Settings, Data Management Area, select Buil Record Deletion and create new job
Define your query as to which records you want to delete
Preview and See if you are deleting the correct set of records
Select the time when you want to start the bulk deletion job and also select the user who should be sent email when deletion completed.
Finally, submit the job
If you want to manually check the job progress you can see in the system jobs area to see how many deleted so far
Hope this helps
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156