CRM 2011: Programmatically creating a bulk delete job

This weekend while working on new product I realized it would be of tremendous use to have a bulk-delete job remove log records once per month.  After looking around, I noticed that the bulkdeleterequest sample was no longer in the CRM 2011 SDK so I decided to update the bulk delete operation section so show you how it works in CRM 2011.

How it works

The BulkDeleteRequest message will create a bulk delete job that will be automatically executed on a specified interval for a specified set of data.  The job will be executed by the CRM Asynchronous service.

Properties of interest

The following properties need to be set:

JobName This is the name of the job which will be displayed on the Bulk Record Deletion job list
QuerySet This is a collection of QueryExpressions that CRM will use to determine what records to delete.

Note: my example is not real-world.

StartDateTime This is the start date for the job. If you don’t specify this date, the date and time the job was created will be used.
RecurrencePattern How frequently do you wish to run this job. More information about the possible recurrence patterns can be found here: Recurrence Pattern in Asynchronous Job Execution
SendEmailNotification Is set to true if you would like to send an email to someone when the job has completed.
ToRecipients List of CRM users to send an email to.
CCRecipients List of CRM users to CC when the email is sent.

Note: this field needs to be specified, even if it is empty.

 

The code

 

string jobName = "Sample Bulk Delete";
int interval = 30;
string frequency =
    String.Format(System.Globalization.CultureInfo.InvariantCulture, 
                  "FREQ=DAILY;INTERVAL={0};", interval);
//
// Query for a system user to send an e-mail 
// to after the bulk delete operation is completed.
//
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse userResponse = (WhoAmIResponse)service.Execute(userRequest);
Guid currentUserId = userResponse.UserId;

QueryExpression query = new QueryExpression
{
    EntityName = "email",
    ColumnSet = new ColumnSet("activityid"),
    Criteria = new FilterExpression
    {
        Conditions =
{
    new ConditionExpression
    {
        AttributeName = "createdon",
        Operator = ConditionOperator.OlderThanXMonths,
        Values = { 1 }
    }
}
    }
};

BulkDeleteRequest bulkDeleteRequest = 
    new BulkDeleteRequest { JobName = jobName, 
                            QuerySet = new QueryExpression[] { query }, 
                            StartDateTime = DateTime.Now, 
                            RecurrencePattern = frequency, 
                            SendEmailNotification = true, 
                            ToRecipients = new Guid[] { currentUserId }, 
                            CCRecipients = new Guid[] { } };

service.Execute(bulkDeleteRequest);

 

Additional Information

Unfortunately, once a bulk deletion job has been created, it can’t be changed easily, if at all.  It’s easier to just delete it and recreate it.

Here is how you can view your recurring system jobs from the CRM web interface:

1) Select Settings, Data Management.

2) Click Bulk Record Deletion.

3) Change your view to Recurring Deletion System Jobs.

 

References

BulkDeleteRequest Members

Leave a Reply 1 comment