Sub topic 3
Matching Across Objects, Such as Leads Against Contacts
Assume you understand Salesforce objects, fields, and basic Apex. A matching rule decides whether records resemble each other. A duplicate rule applies that decision during create or edit.
Matching across objects
A duplicate rule for Leads can use a matching rule that compares Lead fields with Contact fields. For example, it can compare normalized email addresses. A match alerts the user, but it does not merge the Lead and Contact. They remain different object types.
Activating, editing, and ordering rules
Activate a rule only after testing its matching rule and conditions. Edit the rule to change its object, create or edit operations, action, or sharing behavior. When active rules overlap, their order affects which rule is evaluated first. Keep the most specific rule ahead of broad rules.
Reviewing duplicate record sets and reports
Salesforce can store detected matches in Duplicate Record Sets. A Duplicate Record Item connects one record to a set. Review these sets in Salesforce reports or with SOQL:
SELECT Id, DuplicateRuleId, RecordCount
FROM DuplicateRecordSet
ORDER BY CreatedDate DESC
LIMIT 20Merging the duplicates you find
Choose a master record, compare field values, and merge the other records into it. Merging can move related data and removes the duplicate records. A Lead matched to a Contact cannot be merged across objects, so handle that relationship with a conversion or an agreed data-cleanup process.
Finding duplicates in Apex
Datacloud.FindDuplicates runs active duplicate rules against an unsaved record:
Contact candidate = new Contact(
FirstName = 'Jon',
LastName = 'Smith',
Email = '[email protected]'
);
List<Datacloud.FindDuplicatesResult> results =
Datacloud.FindDuplicates.findDuplicates(
new List<Contact>{ candidate }
);
for (Datacloud.FindDuplicatesResult result : results) {
for (Datacloud.DuplicateResult duplicate :
result.getDuplicateResults()) {
System.debug(duplicate.getMatchResults());
}
}Handling duplicate errors and bypassing rules in DML
With partial-success DML, inspect Database.DuplicateError:
Account accountRecord =
new Account(Name = 'Acme Corporation');
Database.SaveResult saveResult =
Database.insert(accountRecord, false);
for (Database.Error error : saveResult.getErrors()) {
if (error instanceof Database.DuplicateError) {
Database.DuplicateError duplicateError =
(Database.DuplicateError) error;
System.debug(duplicateError.getDuplicateResult());
}
}DuplicateRuleHeader.allowSave = true acknowledges an alert-based rule. Use it only for an intentional integration path, not as a replacement for cleanup.
Testing and troubleshooting rules
Test new and edited records separately. Try exact matches, fuzzy matches, blank fields, and records outside the expected sharing scope. If a record is not flagged, check that the rule is active, the operation is enabled, field mappings are correct, conditions pass, and the matching rule supports the object pair.
Design best practices for duplicate prevention
- Start with a standard matching rule when it fits the business data.
- Use a custom rule when standard fields or matching behavior are insufficient.
- Alert users before introducing a blocking rule.
- Normalize email, phone, and address data before matching.
- Review false positives and false negatives regularly.
- Give integrations a documented exception path and log every bypass.
Exercise
Can Salesforce merge a Lead that matches an existing Contact?
Show answer
No. The duplicate rule can detect the cross-object match, but merging works within supported record types, not between a Lead and a Contact.