Feb 122009

CRM 4.0has an endless amounts of system job logs. We perform automatic actions on a lot of entities, but we don’t need the log of those automatic actions beyond the end of that working day if it was Canceled or Completed Successfully. Unfortunately, setting up a bulk delete task for system job logs is not as easy as setting up other bulk delete jobs. I modified the code from Sean McNellis’ blog entry on leveraging bulk delete jobs to manage system job log records.

Below is my code (which is a modified version of the runBulkDelete from the Microsoft Bulk Delete Sample for deleting successfully completed or canceled jobs:

        static void runBulkDeleteAsyncoperations(CrmService service)
            // Create a query expression using the QueryExpressionHelper
            QueryExpressionHelper expression = new QueryExpressionHelper("asyncoperation");
                        new object[]{(int)AsyncOperationStatus.Canceled,(int)AsyncOperationStatus.Succeeded});
            Guid[] emptyRecipients = new Guid[0];
            BulkDeleteRequest request = new BulkDeleteRequest();
            request.JobName = "Bulk Delete Successfully Completed or Canceled Asyncoperations and Workflow Logs";
            request.QuerySet = new QueryBase[] { expression.Query };
            request.ToRecipients = emptyRecipients;
            request.CCRecipients = emptyRecipients;
            request.SendEmailNotification = false;
            request.RecurrencePattern = "FREQ=DAILY;INTERVAL=1;";
            request.StartDateTime = CrmDateTime.Now;

            BulkDeleteResponse response = (BulkDeleteResponse)service.Execute(request);

            Console.WriteLine("Bulk delete job with id: {0} has been created", response.JobId);

Their code examples use a month time span, and if you are trying to delete everything older than 3 days, then you need to build an executable and schedule it to run daily to create the bulk deletion job. You will also need the following filter to be added to the code, and set the RecurrencePattern equal to string.empty.

expression.Criteria.Conditions.AddCondition("completedon", ConditionOperator.OnOrBefore, DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"));

If by some chance you are curious about creating Bulk Delete Jobs in general, check out msdn blog entry.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>