- SalesforceChaCha
- Posts
- 💃 The Art of the Double Take 🕺
💃 The Art of the Double Take 🕺
Mastering Matching Rules and Duplicate Rules in Apex
Good morning, Salesforce Nerds! Let’s face it, in the wild world of Salesforce data, duplicates are like that unwanted encore in a bad concert – they keep showing up, and no one’s happy about it. 😡
But fear not!
Salesforce Matching Rules and Duplicate Rules are here to save your data’s dignity, keeping your org clean and pristine. 🧼
In this article, we’re diving into the art and science of working with these powerful rules in Apex, showing you how to outsmart duplicates before they even think about sneaking in.
Ready to make your database see clearly, no doubles in sight? Let’s go! ✊

TABLE OF CONTENTS
The Art of the Double Take
DOUBLE VISION
Introduction
Salesforce Matching Rules and Duplicate Rules are like that pair of eagle-eyed security guards who never let any double-crossers slip by. 🔐
Whether you’re cleaning up rogue duplicates in legacy data or you’re on the front lines of data integrity as records flow into your system in real-time, these rules are invaluable.
But did you know you can actually control them using Apex? 🤔
This article dives into how to supercharge Salesforce’s Matching and Duplicate Rules with some Apex wizardry.
GET TO KNOW YOUR TOOLS
Understand Matching & Duplicate Rules
Let’s start with the basics: Matching Rules are the brains behind finding potential duplicates. 🧠
They set the criteria, from phone numbers to fuzzy logic on names, that Salesforce uses to say, “Hey, these records look suspiciously alike.”
Then we have Duplicate Rules, the bouncers deciding what to do with those matches – block, alert, or allow but report. 💪
When combined, these rules create a robust framework for data deduplication, but things get interesting when we add Apex into the mix.
LET’S GET HANDS ON
Invoking Matching and Duplicate Rules with Apex
You can call Duplicate Rules manually with Apex using the Datacloud.DuplicateResult
class.
Let’s check out an example where you’ll see how to check if new Contacts being inserted match any existing records, giving you the power to either:
⛔️ Block
⚠️ Alert
🎛️ Manage accordingly
public static List<SObject> findDuplicateRecords(List<Contact> records) {
List<Datacloud.FindDuplicatesResult > findDuplicatesResult = Datacloud.FindDuplicates.findDuplicates(records);
Datacloud.DuplicateResult duplicateResult = findDuplicatesResult.get(0).getDuplicateResults().get(0);
Datacloud.MatchResult matchResult = duplicateResult.getMatchResults()[0];
List<Datacloud.MatchRecord> matchRecords = matchResult.getMatchRecords();
List<SObject> duplicates = new List<SObject>();
for(Datacloud.MatchRecord matchRecord :matchRecords) {
SObject duplicate = matchRecord.getRecord();
System.debug('Match Record: ' + JSON.serializePretty(duplicate));
duplicates.add(duplicate);
}
return duplicates;
}
Here, Datacloud.findDuplicates()
kicks off the Matching and Duplicate Rules against the Contact list, returning a DuplicateResult
that contains any suspect records.
This allows you to decide what to do with them – log an error, throw a friendly alert, or even update the record with some metadata for further processing.
💃 ChaCha’s Pro Tips 🕺
🛑 Mind the Limits: Matching and Duplicate Rules are powerful, but there’s a limit of 50 records per Datacloud.findDuplicates()
call. For larger datasets, batch your calls to avoid hitting governor limits.
🎮️ Get Fine-Grained Control with Apex: Use Datacloud.DuplicateResult
to check if a duplicate was blocked, allowed, or simply alerted on. This status can be super useful for logging or reporting purposes.
📈 Monitor for Accuracy: Duplicate Rules aren’t “set-it-and-forget-it” – over time, tweak and tune the rules as your data quality demands evolve.
MATCH ON YOUR TERMS
Customizing Matching Logic
Salesforce’s out-of-the-box Matching Rules are pretty smart, but maybe your organization has its own ideas on what constitutes a duplicate. 💯
This is where custom Matching Rules come in.
You can create Matching Rules based on custom fields or even tweak the “fuzziness” on standard fields. 😁
Let’s say you’re working for a healthcare company, and they want a rule to match patients based on their insurance number and birthdate.
You can create this rule in the Matching Rules setup page and then use Apex to find potential duplicates based on these custom criteria. 🤯
WRAPPING IT UP
Rules Rule!
Matching Rules and Duplicate Rules are incredible allies in the war on duplicate data. 🪖
And while they’re already powerful on their own, bringing Apex into the equation puts you fully in control, allowing you to create custom solutions tailored to the unique needs of your org.
As you continue refining your duplicate strategy, don’t be afraid to experiment with custom logic, leverage findDuplicates()
, and set up logging to track your progress.
A clean database isn’t just a happy one – it’s efficient, reliable, and easier to manage. So go forth, and let Apex make your data deduplication dreams come true! 🫡
SOUL FOOD
Today’s Principle
"I begin to speak only when I’m certain what I’ll say isn’t better left unsaid.”
and now....Salesforce Memes



What did you think about today's newsletter? |