My CRM has over 100.00 duplicate contacts. Manually merging duplicates will take forever. Any ideas how to quickly and cleanly eliminate the duplicates? Help!!
*This post is locked for comments
My CRM has over 100.00 duplicate contacts. Manually merging duplicates will take forever. Any ideas how to quickly and cleanly eliminate the duplicates? Help!!
*This post is locked for comments
Hi Pegineb,
CRM out of box duplicate detection works well in detecting duplicates espcially if there are less than 5k records and merging them will have to be a manual job of going through and merging them via UI.
I had similar requirement to merge over 100k account records and i wrote a small console app to do that job a while ago.
Summary of How I Achieved this.
01. I first retrievesd all the accounts - In my case i only did the merging for a Country at a time, i filtered the list of accounts i retrieved from CRM to be only from that country - This reduces list of records we process at a time. When you retrieve, you will have to use paging to get all the records, something similar to the following
private List<Entity> RetrieveEntitiesPagedResult(QueryExpression query)
{
List<Entity> entities = new List<Entity>();
query.PageInfo = new PagingInfo();
query.PageInfo.Count = 5000;
query.PageInfo.PageNumber = 1;
query.PageInfo.ReturnTotalRecordCount = true;
EntityCollection results = null;
do
{
results = CrmService.RetrieveMultiple(query);
if (results.Entities.Count >= 0) entities.AddRange(results.Entities);
if (!results.MoreRecords) continue;
query.PageInfo.PageNumber += 1;
query.PageInfo.PagingCookie = results.PagingCookie;
} while (results.MoreRecords);
return entities;
}
02. In my code I then grouped the retrieved records by Account Name
var groups = AccountsToBeProcessed.GroupBy(c => c.Name).Select(group => new {
Name = group.Key,
Count = group.Count(),
Accounts = group.ToList()
})
.OrderBy(x => x.Name);
03. From the grouped list, i then used the following query to get the get the duplicates
var duplicateGroups = groups.Where(x => x.Count >= 2 && x.Accounts.Any(y => !string.IsNullOrEmpty(y.internalID)) && x.Accounts.Any(z => z.CompanyID == "France")).OrderBy(x => x.Name).ToList();
04. The code then loops through the reulting list of duplicate account groups, decides that one to keep as master and merge the other one to it using MergeRequest docs.microsoft.com/.../gg308057(v%3Dcrm.8)
05. Finally the actual MergeRequest ...
You can only merge one subordinate master in one MergeRequest. It means if you have more than 2 in any of the above mentioned duplicate group, you have to cater for it. By doing multiple merge requests.
When you execute the MergeRequest you will have to define the following
MergeRequest merge = new MergeRequest();
// Set the Master
EntityReference target = duplicate.CRMAccount.ToEntityReference();
merge.Target = duplicate.CRMAccount.ToEntityReference();
// SubordinateId is the GUID of the account merging.
merge.SubordinateId = duplicate.ChildAccount.Id;
// This will force the Parenting check
merge.PerformParentingChecks = true;
You will then have to provide what fields you want to copy from Child to Master.
For Example I wanted to copy the primary contact of the Child to Master if the master did not have Primary Contact Set
Entity updateContent = new Entity("account");
if (!MasterAccount.Contains(AccountFields.primarycontactid) && duplicate.E8Account.Contains(AccountFields.primarycontactid))
{
updateContent.Attributes.Add("primarycontactid", new EntityReference("contact", ChildAccount.GetAttributeValue<EntityReference>
("primarycontactid").Id));
}
I also wanted to copy Company ID from Child to Master - if the master already had it will replace with what child had
updateContent.Attributes.Add("new_companyid", duplicate.E8Account.Attributes["new_companyid"]);
//Set the update content to merge request
merge.UpdateContent = updateContent;
And then execute the MergeRequest
MergeResponse mergeRes = (MergeResponse)CrmService.Execute(merge);
All the child records and activities will be reparented to master as long as the correct relationship behaviour is set.
Hope this heps.
When merging more than one record, what business rule would you use to attach all the details from the old record to the primary record? We want to make sure all our posted forms, sent emails, etc. are attached to one record.
Define the business rules to merge and implement a console application msdn.microsoft.com/.../gg695803.aspx which will retrieve all the contacts, compare based on the defined rules and merge those records using MergeRequest msdn.microsoft.com/.../hh547408.aspx
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
52
dkrishna
6