fbpx

Even when Salesforce users follow standard data cleanliness best practices outlined in the Salesforce guide here, duplicates are simply unavoidable in some circumstances. Every single Salesforce org has an issue with duplicates, to one degree or another, so you’re not alone. As much as we’d love it, there is no silver bullet for duplicates. As long as you’re working with and collecting data, your organisation will need a process to dedupe it.

Why Duplicates Occur

In spite of all the technology available to us, duplicates occur because computers don’t possess the same cognitive abilities as humans, although recent advancements in AI certainly hlp. Computers operate on a tight set of rules. Accurately determining what a duplicate is in a CRM, sometimes requires human oversight. So expect deduping to be a regular internal process.

If you’ve already used a free tool like Dupe Dive to locate duplicates in your Salesforce org, but still need help, this guide is for you. Today, we’ll be going deeper to share our advanced tips for Salesforce Administrators who are familiar with Apex and are ready to take this to the next level.

Using Apex for Duplicate Management

You can now use the standard Salesforce duplicate rules in Apex. The benefits include the following:

  • Fuzzy matching on contact’s first name
  • Matching rules can be configured by the system administrator in Setup instead of being hardcoded in Apex

1. Use findDuplicates

The duplicate results are displayed as a complex hierarchy of objects.

findDuplicates() gives you a list of Datacloud.FindDuplicatesResult objects, in which each element corresponds to the element in the list that was passed in. Thus, if a contact list was passed in, the fdresults[0] corresponds to contacts[0], fdresults[1] to contacts[1], and so on.

findDuplicatesResult has a success property, however, this isn’t related to active/inactive rules. It tends to be used for other kinds of errors that are not described in the documentation here.

Every findDuplicatesResult object contains a list of DuplicateResult objects. Every DuplicateResult object corresponds to an active duplicate rule for that object.

Every DuplicateResult record contains a MatchResults object.

Finally, the MatchResults object contains a list of MatchRecord objects. And in MatchRecord, the record contains the actual matched Object record.

2. Take Note of Which Fields Are Returned in the MatchRecord

findDuplicates returns only the fields indicated in the primary CompactLayout associated with the target object. Currently, it’s not possible to change this. So you must execute a separate query to get fields that aren’t in the CompactLayout.

3. Dealing with Multiple Duplicate Rules for An Object

This tends to get rather complex, as you’re able to get multiple lists of MatchRecords returned, especially if the lists don’t have the same records and/or they’re not in the same order. You’ll need to figure out how to select the “winning” match from different lists, which will depend on your specific set of duplicate rules.

4. Detecting Whether Duplicate Rules Are Active

There’s currently no elegant method for detecting whether there are any active Duplicate Rules. The only way to know is to execute the Datacloud.FindDuplicates.findDuplicates() method. If there aren’t any active rules, then an exception will occur, and you’ll need to catch it.

The exception message is, “System.HandledException: No active duplicate rules are defined for the [objname] object type”.

5. Override Duplicate Management Rules

When you need to override all of the duplicate management rules, set the allowSave property in the DmlOptions.DuplicateRuleHeader class.

Database.DMLOptions dml = new Database.DMLOptions();

dml.DuplicateRuleHeader.allowSave = true;

You can read the official documentation here for more details.

6. Bypass Sharing Rules As A Site Guest User

The Site Guest User can use a duplicate process, but the duplicate rules must be set to Bypass Sharing so that all Contacts will be available for matching.

Some Examples of findDuplicates() with Different Rules

The 2 active Contact duplicate rules are:

  • Rule 1 checks for first name fuzzy / last name / email
  • Rule 2 checks for first name fuzzy / last name / zip code

 There are 3 existing contacts:

  • Name = John Doe, Email = jdoe@test.com, Zip Code = 12345
  • Name = John Doe, Email = jd@test.com, Zip Code = 12345.
  • Name = Jack Doe, Email = jack@test.com, Zip Code = 12345.

 There are 2 contacts in the search list:

  • Name = Jack Doe, Email = jack@test.com, Zip Code = 12345.
  • Name = John Doe, Email = somebody@test.com, Zip Code = 99999.

Datacloud.FindDuplicates.findDuplicates will return a list of two elements.

fdResults[0] will contain two lists of DuplicateResults

fdResults[0].dupResults[0] is Rule 1. There will be 1 MatchResult.

fdResults[0].dupResults[1] is Rule 2. There will be 3 MatchResults.

fdResults[1] will contain two lists of DuplicateResults

fdResults[1].dupResults[0] is Rule 1. There will be 0 MatchResults.

fdResults[1].dupResults[1] is Rule 2. There will be 0 MatchResults.

 

 

Further Reading

By clicking “Continue” or continuing to use our website, you acknowledge that you accept our Privacy Policy. We will not track your information when you visit our website. We only use cookies to provide you with the best possible experience. Feel free to check out our policies anytime for more information.Continue
Bitnami