The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

Synchronous Matchcode Generation - Forced Duplicate Detection in Dynamics CRM 4

Synchronous Matchcode Generation - Forced Duplicate Detection in Dynamics CRM 4

  • Comments 8

Today we welcome our guest blogger and CRM MVP Ayaz Ahmad. Ayaz blogs at Ayaz Ahmad [MVP MIcrosoft CRM, MBSS].

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. The 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.

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.InputParameters.Properties[ParameterName.Target] = null;

Cheers,

Ayaz Ahmad

  • PingBack from http://blog.a-foton.ru/index.php/2009/03/27/synchronous-matchcode-generation-forced-duplicate-detection-in-dynamics-crm-4/

  • Hello

       Why 'context.InputParameters.Properties[ParameterName.Target] = null' statement is outside of 'if ((message.ToLower() == "create"))'? Shouldn't it be inside? If not please correct me.

  • Which field in the entity stores the computed match code value for the newly/modified records?

    For example, say contact entity is populated with records, which field in ContactBase table represents the matchcode value that is used by matchcode service while running duplicate detection?

  • Which field in the entity stores the computed match code value for the newly/modified records?

    For example, say contact entity is populated with records, which field in ContactBase table represents the matchcode value that is used by matchcode service while running duplicate detection?

  • The matchcodes are not stored in the entity table. They are stored in a separate table created when you publish a rule. The name of the table is stored in the duplicate rule entity. There could be one or two such tables for a rule based on whether it is a same entity rule or a cross entity rule.

    E.g. If you have a rule which says contact is a duplicate of contact if last name is same, there will be one table created when you publish the rule and its name can be found in the duplicate rule entity instance.

  • well, you can simply replace the last statement

    context.InputParameters.Properties[ParameterName.Target] = null

    with

    context = null;

    Otherwise your plugin will execute normally. I am not sure, is there any side effects of setting context = null. But it works.

  • Where exactly would this code get inserted?  I've tried it on the onSave event, but it does not work.  

    I'm not sure what is meant by "write a plugin for this at Create and Update message".  Can you elaborate?

    Thanks.

  • I am new to CRM and wnated to know, If CRM can match the records based on the Key words, not the exact words

Page 1 of 1 (8 items)
Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post