Sub topic 10
Matching across objects, such as Leads against Contacts
This chapter assumes you know how a matching rule and a duplicate rule fit together, and that you have built a custom matching rule with a matching equation. Here you will see how a duplicate rule compares records of one object with records of a different object.
Why compare across objects
Duplicates do not always sit inside one object. A sales rep creates a Lead for someone who is already a Contact on a customer Account. Marketing imports a list of Leads that includes people your support team already tracks as Contacts. A same-object rule cannot see either problem, because the Lead and the Contact live in different tables.
A cross-object duplicate rule fixes this. The rule runs when a record of one object is saved, but it searches for matches in another object. The most common pairings are:
- A new or edited Lead compared against existing Contacts.
- A new or edited Contact compared against existing Leads.
- Account-related comparisons, such as Leads against Accounts or Contacts against Accounts. Which of these are available depends on your org's features, for example person accounts.
This chapter uses Lead and Contact as the running example. The same ideas apply to the other pairings.
How a cross-object rule is built
A same-object duplicate rule has one object and one matching rule. A cross-object rule has two sides:
- The source object is the object the rule belongs to, and the one whose save triggers the check.
- The target object is the object whose existing records are searched for matches.
A duplicate rule lists one or more matching rules. Each entry names an object to compare against and the matching rule that applies to it. The first entry usually compares the object with itself. Add a second entry to compare with another object, such as Contact.
Two points about this design come up often:
- The matching rule belongs to the target object. To find Contacts that match a Lead, the matching rule must be a Contact matching rule, built on the Contact object.
- The duplicate rule belongs to the source object. Setup shows it under the Lead duplicate rules, even though it reports Contact matches.
Standard rules and field mapping
Salesforce provides standard rules for the common pairing. A standard Lead duplicate rule can compare Leads with Contacts, using the standard matching rules for those objects. Salesforce handles the comparison of fields such as name, email, company and phone for you. Standard rules are often enough for a first rollout, as covered earlier in this tutorial.
With a custom matching rule, you must tell Salesforce which source field lines up with which target field. This is field mapping. A Lead has Company and Email. A Contact has Email and an Account relationship instead of a company text field. Salesforce cannot guess that Lead.Company should be compared with the Contact's account name. You define the mapping in the duplicate rule so each field in the target matching rule has a corresponding source field.
| Lead field (source) | Contact field (target) | Comment |
|---|---|---|
| FirstName | FirstName | Direct match, often fuzzy |
| LastName | LastName | Direct match, often fuzzy |
| Strong identifier, usually exact | ||
| Company | Account name | Different shape, needs explicit mapping |
| Phone | Phone | Normalize formats where possible |
Mapping matters for correctness. If a mapping pairs two fields that do not hold the same kind of value, the rule runs without error but produces poor matches. Check the mapping carefully when you build the rule, and check it again when you test.
Building a Lead-to-Contact rule
The steps below describe the flow in Setup. Labels can vary slightly between releases.
- Open Setup, then Duplicate Rules, and create a new rule (or clone the standard Lead rule if you want to keep its other settings).
- Choose Lead as the object. Name the rule, for example
Lead - Check Leads and Contacts. - Set the action for create and edit, as in the earlier chapter on actions. For cross-object checks, Alert is the common starting point, because a Lead matching a Contact is often legitimate.
- In the matching rules section, add a rule that compares Leads with Leads.
- Add another entry that compares with Contacts, and pick a Contact matching rule.
- If the matching rule is custom, open the field mapping and align each target field with a source field.
- Save, then activate the rule. Activation is covered in a later chapter.
A matching rule must be active before a duplicate rule can use it. Activate the Contact matching rule first.
Actions behave differently across objects
Consider what each action means when the match is in another object.
- Block stops the Lead from being saved because a Contact exists. This can frustrate users, since the correct response may be to work the existing Contact. Use Block across objects only when the business really never wants a Lead for an existing Contact.
- Alert shows the matching Contacts and lets the user continue. This is the usual choice, and it lets the rep see the existing relationship.
- Report creates a duplicate record set without interrupting the user. It is a good first step if you are not sure how many cross-object matches your data holds.
Sharing and visibility still apply. If the rule is set to enforce sharing rules, a user will only be alerted about Contacts they can see. A hidden Contact can be an actual duplicate that the rule does not report to that user. Decide on this deliberately, as discussed in the chapter on scope and sharing.
Duplicate record sets across objects
When a cross-object rule fires with Report or an alert, the duplicate record set can hold records of different types. The set contains a Lead item and a Contact item. You can see the mix by querying the items and checking which object each record belongs to:
SELECT Id, DuplicateRecordSetId, RecordId
FROM DuplicateRecordItem
ORDER BY CreatedDate DESC
LIMIT 20RecordId is a generic reference. The record ID prefix tells you whether the item is a Lead (00Q) or a Contact (003). Reports on duplicate record items can also show which object each item belongs to.
Merging has limits here. Salesforce merges records of the same object. Two Leads can merge into one Lead, and two Contacts into one Contact, but a Lead and a Contact do not merge into each other. The usual way to resolve a Lead-Contact match is to convert the Lead. On conversion you pick the existing Contact to attach the Lead to, and the Lead's data is merged into the Contact.
Finding cross-object matches in Apex
Datacloud.FindDuplicates runs the active duplicate rules for the record's object. If the Lead duplicate rule includes a Contact comparison, matches in Contacts come back in the same result structure. Check the object type of each match:
Lead candidate = new Lead(
FirstName = 'Jon',
LastName = 'Smith',
Company = 'Acme Corporation',
Email = '[email protected]'
);
List<Datacloud.FindDuplicatesResult> results =
Datacloud.FindDuplicates.findDuplicates(new List<Lead>{ candidate });
for (Datacloud.FindDuplicatesResult findResult : results) {
for (Datacloud.DuplicateResult dupResult : findResult.getDuplicateResults()) {
for (Datacloud.MatchResult matchResult : dupResult.getMatchResults()) {
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
SObject match = matchRecord.getRecord();
System.debug(
dupResult.getDuplicateRule() + ' matched a '
+ match.getSObjectType() + ': ' + match.Id
);
}
}
}
}The debug line prints Lead or Contact for each match, so your code can respond differently to each case.
Common problems
- A Lead is not flagged against a Contact. Check that the duplicate rule has a Contact entry, that its matching rule is active, and that the field mapping is correct. Then check whether the Contact is visible to the user when sharing is enforced.
- Too many Lead and Contact matches. Fuzzy name matching with no second field is the usual cause. Add an email or company condition to the Contact matching rule.
- Contacts are flagged against Leads, but not the other way. Each direction is a separate duplicate rule. The Contact rule and the Lead rule must each be set up and activated.
- Imports skip the check. Data Import tools and some API calls do not trigger alerts the same way. Use Report, or a later review of the duplicate record sets.
Exercise
A rep creates a Lead for a person who already exists as a Contact. You want the rep to see the Contact but still be able to save the Lead. On which object do you create the duplicate rule, which object does its matching rule belong to, and which action do you choose?
Show answer
Create the duplicate rule on Lead, because the Lead save triggers the check. Add a comparison against Contacts, using a Contact matching rule, because the matching rule belongs to the target object. If the matching rule is custom, map the Lead fields to the Contact fields. Choose the Alert action. It shows the matching Contact and lets the rep continue. Block would stop the save.
Exercise
A duplicate record set contains one Lead and one Contact. Can you merge them with the standard merge tool? If not, what is the usual way to resolve it?
Show answer
No. Merging works between records of the same object. Convert the Lead instead, and attach it to the existing Contact during conversion.