Home > MSCRM General > Synchronous Matchcode Generation – Duplicate Detection in Dynamics CRM 4

Synchronous Matchcode Generation – Duplicate Detection in Dynamics CRM 4

Microsoft Dynamics CRM 4.0 provides a Duplicate Detection feature to protect the quality of your data. Duplicate Detection rules can be defined for different record types, including custom entities, and can be defined across record types as well.

In background a matchcode service is running and creating/updating matchcode for new/existing records. This matchcode will be matched to detect duplicates. Matchcode service runs asynchronously after 5 minutes and create/updates matchcode. So any record that is inserted within 5 minutes of time will not be detected. This happens only when you insert record within less than 5 minutes of time or before generation of matchcode. This results in no warning message for duplicate records even though duplicate record exists according to duplicate rules.

There is no way to persist matchcodes synchronously when records are created/updated through the UI, without writing extra code. So here are few recommendations to do so.

  1. A workaround is to Import data (Tools->Import), and choose the option to prevent creation of duplicate records. This will create the matchcodes for all imported records synchronously.
  2. You can force the creation of a matchcode at the time of record create/update by including the PersistInSyncOptionalParameter in the Create/Update request and setting its value to ‘True’. A code snippet that includes the optional parameter in a CreateRequest is as follows:
    CreateRequest request = new CreateRequest();
    request.OptionalParameters = new OptionalParameter[] { new PersistInSyncOptionalParameter (true) };

  3. You can write a plugin for this at Create and Update message. Beware to silent your plugin by setting Context to null.

if (context.Depth <= 1)
                    if ((message.ToLower() == “create”))
                        DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

                        TargetCreateDynamic createdynamic = new TargetCreateDynamic();
                        createdynamic.Entity = entity;                      

                        CreateRequest request = new CreateRequest();
                        request.Target = createdynamic;
                        request.OptionalParameters = new OptionalParameter[] { new PersistInSyncOptionalParameter(true) };

                        CreateResponse response = (CreateResponse)service.Execute(request);
                context = null;

  1. May 20, 2009 at 2:13 am

    We have set up Duplication Detection setting rules and have published them, on email address and telephone within the Account and Contact forms.

    Our issue is when it detects a possible duplication, if you select Save to continue saving the record, it does not create the new Contact or Account. There is no indication that nothing has happened until you go to search for the new record and it does not exist.

    I cannot find any information online to analyse and resolve this problem. Do you have any suggestions.

  2. June 26, 2009 at 6:42 am

    Nice writeup!

  3. Saurabh
    October 9, 2009 at 7:03 am

    does the duplicate detection rules work when converting a lead to an account and contact?

    Please confirm.

    Saurabh Lal

  4. October 9, 2009 at 8:51 am

    I am afraid not. but you can intruppt the process by writing a plugin and validate the account/contact/opp records at create time.

  5. Asim
    April 16, 2010 at 12:57 pm


    I am importing 200 records, On create of each record there is a workflow which is creating another record for some other entity, I am using this optional parameter which you specified while creating another record, but the issue is i am still getting few duplicates dont know why but maybe because of parallel calls because workflow is running async, like from 200 records i got 5 duplicates

    any suggestions ?


  6. shane Baartman
    October 11, 2010 at 12:53 pm

    Two questions if I may

    is the service from the code example when service.Execute(request) is it the ICrmService service = context.CreateCrmService(true); service cause the sdk service can’t set optionalparameter. secondly I would love to see this implemented and working correctly cause with the duplicate detection rule running duplicate records can be created without warning and would return the newly created as a duplicate (itself) – still creating two records everytime.

  1. No trackbacks yet.

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 )

Connecting to %s

%d bloggers like this: