[CRM 4] Bulk Deletion Jobs tool

A Bulk Deletion Job is an asynchronous operation in CRM to delete a set of records according to a custom query. This is very useful when you have a CRM where hundreds of users are inserting and editing records constantly and you want to keep the performance of your CRM database.

In CRM 4, It’s not possible to create Bulk Deletion Jobs manually from CRM web interface (or CRM Outlook client). However, I’ve created a tool, which can be integrated in CRM 4, to provide this functionality. As you can see below, if you go to Data Management –> Bulk Record Deletion, now there is a new button called “New Job”:

When you click there, the next form will appear:

To select the set of records that you want to delete, you can specify a System View or a Fetch XML.

Behind this, when you press “Created Job”, the main code to be executed is something like:

public static void CreateBulkDeletionJobInCrm(string jobName, DateTime startDateTime, string frequency, int interval, QueryExpression query)
    {
        BulkDeleteRequest bulkDeleteRequest = new BulkDeleteRequest();
        // Set the request properties.
        bulkDeleteRequest.JobName = jobName;
        bulkDeleteRequest.QuerySet = new QueryBase[] { query };

        // Set the start time for the bulk delete.
        bulkDeleteRequest.StartDateTime = PropertyHelper.GetUserDateTime(startDateTime); 

        // Set e-mail activity properties. So far, these properties are not used, but
        //they should be initialised
        bulkDeleteRequest.SendEmailNotification = false;
        bulkDeleteRequest.ToRecipients = new Guid[] { };
        bulkDeleteRequest.CCRecipients = new Guid[] { };

        // Provide a recurrence pattern so that the job runs once every day.
        if(string.IsNullOrEmpty(frequency))
            bulkDeleteRequest.RecurrencePattern = string.Empty;
        else
            bulkDeleteRequest.RecurrencePattern =
                String.Format(System.Globalization.CultureInfo.InvariantCulture,
                "FREQ={0};INTERVAL={1};", new object[] { frequency, interval });
        

        ServiceFactory.CrmService.Execute(bulkDeleteRequest);

    }

The last parameter of this method is a Microsoft.Crm.Sdk.Query.QueryExpression. To get it from a System View or FetchXML, you can use  FetchXmlToQueryExpressionRequest/Response.

Although I would like to provide you  with the whole solution, it’s one of the products of my current company AlfaPeople Ltd, so that you should email me or contact us from our website.

Advertisements

Posted on October 5, 2009, in CRM. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: