Sub topic 9
Converting into existing accounts and contacts, and duplicate matching
Prerequisites
This chapter assumes you know how a standard lead conversion works. You should know that it creates an account, a contact, and optionally an opportunity, and that field mapping copies lead values onto those records. You should also be comfortable reading basic Apex and SOQL, and know what a duplicate rule is at the Setup level.
Why conversion often should not create new records
The default conversion creates a new account and a new contact. That is correct when the lead is a new company and person. It is wrong when your database already holds that company, that person, or both. Converting anyway leaves you with two accounts for one customer, split activity history, and a rep who can't see the full relationship.
Salesforce gives you two mechanisms to prevent this:
- Choosing existing records at conversion time, either in the Convert Lead screen or through Apex.
- Duplicate management, meaning matching rules and duplicate rules that warn about or block the creation of duplicates.
Choosing existing records in the Convert Lead screen
In Lightning Experience, the Convert Lead screen has sections for Account, Contact, and Opportunity. Salesforce searches for records that look like the lead and offers them in each section. For each section you choose one of two paths:
- Create New. Salesforce creates the record and applies your lead field mapping.
- Choose Existing. You pick a record from the matches or search for one yourself, and the lead attaches to it.
The sections depend on each other:
| Your choice | What happens |
|---|---|
| New account, new contact | Both records are created from the lead. This is the default. |
| Existing account, new contact | The new contact is created under the chosen account. |
| Existing account, existing contact | No account or contact is created. The lead attaches to both. |
| Existing contact, new account | Not allowed. A contact must belong to the account you select. |
| Either option, no opportunity | Select "Don't create an opportunity upon conversion". |
The last row of the middle section is the one people trip over. If you pick an existing contact, the account must be the account that contact already belongs to. Pick a different account and the conversion fails.
What happens to data when you convert into an existing record
When you convert into existing records, nothing new is created for those records. Instead the lead's related data moves onto them:
- Activity history (tasks, events, logged calls) from the lead moves to the contact, and the account timeline reflects it.
- Campaign member history carries over to the contact, so campaign influence is preserved.
- The lead is marked converted and keeps references to the records it was converted into (
ConvertedAccountId,ConvertedContactId,ConvertedOpportunityId).
Field values need more care. Mapped lead fields are written to new records, but existing records already hold data that a rep or an integration may have curated. In general Salesforce does not replace populated values on existing records with lead values. The exact behavior depends on the field and your mapping, so test it in a sandbox before you rely on it. Keep in mind that a lead's phone number or title may be newer than the contact's, and the conversion may not update it. If freshness matters, add a post-conversion step (a flow or Apex) that copies chosen fields explicitly.
There is one deliberate exception for Lead Source. The Convert Lead screen and Apex (setOverwriteLeadSource) let you overwrite the Lead Source on the existing contact with the lead's value. Leave this off unless you want the original contact's source replaced.
How matching helps
Matching rules decide whether two records are "the same." Salesforce ships standard matching rules for leads, accounts, and contacts. You can also build custom ones. Typical logic compares:
- Email, using an exact match.
- First and last name, using fuzzy matching such as nicknames and phonetic similarity.
- Company or account name, using fuzzy matching.
- Phone, website, or billing address as supporting fields.
A matching rule does nothing by itself. A duplicate rule connects it to an action. For each object the duplicate rule says which matching rule to use, who it applies to, whether to Allow (optionally with an alert) or Block on create and edit, and whether to log a report entry.
When a lead converts, the account, contact, and opportunity that conversion would create are subject to the duplicate rules for those objects:
- With Allow and an alert, the Lightning Convert screen shows the potential duplicates so the rep can switch to Choose Existing before saving.
- With Block, conversion creating a matching record fails with a duplicate error.
- With Allow and no alert, the duplicate is created silently. Reports are the only way you would find out.
A Block rule on contacts is the strongest guard, but it also makes failures visible to reps who may not know why. Pair it with training and a clear error message.
Lead-to-lead duplicates
Matching also matters before conversion. Two leads for the same person, converted separately, produce two contacts or force a manual merge. A duplicate rule on the Lead object can compare incoming leads against existing leads and contacts. Run it on create so Web-to-Lead and imports are checked. Be careful with Block on Web-to-Lead. A blocked submission may simply be lost, whereas Allow with a report lets you merge later. Salesforce also lets you merge up to three duplicate leads manually from the lead's duplicate list, which is cheaper than cleaning up after conversion.
Converting into existing records with Apex
Database.LeadConvert has setters that mirror the screen choices:
| Method | Purpose |
|---|---|
| `setAccountId(Id)` | Attach to an existing account instead of creating one. |
| `setContactId(Id)` | Attach to an existing contact. Also set the account that contact belongs to. |
| `setOpportunityId(Id)` | Attach to an existing opportunity (an Apex option with no direct screen choice). Do not combine with `setDoNotCreateOpportunity(true)`. |
| `setOverwriteLeadSource(Boolean)` | Replace Lead Source on the existing contact. |
| `setOwnerId(Id)` | Set the owner of the records created. |
| `setSendNotificationEmail(Boolean)` | Notify the new owner. |
The example below looks for a contact with the same email, converts into that contact and its account when found, and creates new records otherwise. It uses the partial-success form of convertLead and reports errors, including duplicate-rule failures.
LeadStatus convertStatus = [
SELECT MasterLabel
FROM LeadStatus
WHERE IsConverted = true
LIMIT 1
];
Lead l = [
SELECT Id, Email
FROM Lead
WHERE IsConverted = false AND Email = '[email protected]'
LIMIT 1
];
List<Contact> matches = [
SELECT Id, AccountId
FROM Contact
WHERE Email = :l.Email AND AccountId != null
LIMIT 1
];
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);
if (!matches.isEmpty()) {
lc.setAccountId(matches[0].AccountId);
lc.setContactId(matches[0].Id);
}
Database.LeadConvertResult result = Database.convertLead(lc, false);
if (result.isSuccess()) {
System.debug('Account: ' + result.getAccountId());
System.debug('Contact: ' + result.getContactId());
} else {
for (Database.Error err : result.getErrors()) {
System.debug(err.getStatusCode() + ': ' + err.getMessage());
}
}The AccountId != null filter matters. A contact with no account (a private contact) cannot be paired with an account selection. Matching by email alone is also a simplification. In production, prefer a shared helper that applies your org's real matching logic, and query in bulk before the loop so you stay within SOQL limits. Database.convertLead accepts a list of LeadConvert objects, up to 100 per call.
Querying the result
After conversion, check what the lead linked to. The query from the previous chapter works here too:
SELECT Id, Name, ConvertedDate, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId
FROM Lead
WHERE IsConverted = true
ORDER BY ConvertedDate DESC
LIMIT 10When you convert into existing records, ConvertedAccountId and ConvertedContactId hold the IDs of those existing records. Comparing their creation dates with ConvertedDate tells you whether a new record was created or an old one reused.
Common errors
- Contact does not belong to the selected account. Choose the contact's own account, or create a new contact.
- Duplicate error on convert. A Block duplicate rule matched the record that conversion tried to create. Choose the matching record as the existing one, or relax the rule.
- Unexpected stale data. Fields on the existing contact kept their old values. Add an explicit update step if the lead's value should win.
- Lead Source changed on a long-standing contact.
OverwriteLeadSourcewas on. Turn it off. - Duplicates created silently. The duplicate rule was set to Allow without an alert. Add the alert or a duplicate report.
- Wrong person matched. The matching rule is too loose or uses a shared email. Tighten the rule or require more fields.
Best practices
- Decide per source. Inbound web leads from existing customers should usually convert into existing records. Cold purchased lists often should not be matched automatically.
- Use Allow with alerts first, review the duplicate report, and move to Block only for rules you trust.
- Test conversions into existing records in a sandbox with realistic data, including contacts that have no account.
- Log or report conversions that did not create new records so you can audit them.
Exercise
A rep tries to convert a lead. She selects an existing contact for "Priya Rao" but leaves the account as "Create New". What happens, and what are two ways to fix it?
Show answer
The conversion fails because a contact must belong to the account selected for conversion, and a new account cannot own an existing contact. Fix 1: choose the contact's own existing account in the Account section. Fix 2: leave Contact as Create New, so a new contact is made under the new account (this would create a second Priya Rao, so use it only if she is really a different person).
Exercise
You set a duplicate rule on Contact to Block. A scripted conversion using Database.convertLead(lc, false) returns isSuccess() == false. Where do you find the reason, and which setter would you add to avoid the failure for a lead whose email matches an existing contact?
Show answer
Read result.getErrors() and check each Database.Error status code and message, which will indicate a duplicate. To avoid it, query for the matching contact and call lc.setAccountId(contact.AccountId) and lc.setContactId(contact.Id) so the lead converts into the existing contact instead of creating a new one.