Automatically Publish Duplicate Detection Rules in CRM 2011

When installing a managed or unmanaged solution for Microsoft Dynamics CRM 2011, extra care must be taken to ensure that all components of the system work as expected. Duplicate Detection rules are not solution components, but if any entity metadata is modified during a solution import, all Duplicate Detection Rules associated with the entity are automatically unpublished. These unpublished rules can often be overlooked and can cause problems for the customer, so what can we do to make sure that the rules are published?

capture1

The obvious answer is to add a step to your deployment process to manually publish Duplicate Detection Rules. An even better solution is to automatically publish these rules when a user clicks on Publish All Customizations. Thankfully, we can do this with a simple plug-in.

private void PublishRules()
{
    // retrieve all duplicate detection rules
    List<Entity> rules = GetDuplicateDetectionRules();

    rules.ForEach(r =>
    {
        _sdk.Execute(new PublishDuplicateRuleRequest
        {
            DuplicateRuleId = r.Id
        });
    });
}

private List<Entity> GetDuplicateDetectionRules()
{
    QueryExpression query = new QueryExpression { EntityName = "duplicaterule" };
    query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);   // inactive
    query.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 0);  // unpublished

    EntityCollection results = _sdk.RetrieveMultiple(query);
    return results.Entities.ToList();
}

The plugin is registered on the Publish All message and simply retrieves all unpublished Duplicate Detection Rules and executes a PublishDuplicateRuleRequest. After we click on Publish All Customizations, we can see that the status of the Duplicate Detection Rules changes to “Publishing”. The process to publish Duplicate Detection Rules can take a long time and depends on the number of rules to publish, as well as the number of active records in the system corresponding to each rule.

capture2

Enjoy!

Advertisements

One thought on “Automatically Publish Duplicate Detection Rules in CRM 2011”

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