Sub topic 5
Matching Existing Records and Avoiding Duplicates
This chapter assumes you can prepare a CSV file, know the required fields on the target object, and have run at least one import with the Data Import Wizard. It covers how the wizard decides that an incoming row matches an existing record, what each matching option does, and how to avoid duplicates when you use Data Loader.
Why matching matters
An import that only adds records is simple. The harder case is a file that contains some people or companies already in Salesforce. If the tool cannot tell which rows are new and which are existing, you get duplicates. Duplicates split activity history across records, inflate reports, and make later cleanup expensive.
Matching is the step where the tool compares a value in each CSV row with values on existing records. If a match is found, the row can update that record. If no match is found, the row creates a new record. Each tool matches differently, so you need to know which tool you are using and which key it relies on.
Wizard actions: add, update, or both
When you start the Data Import Wizard for a standard object such as Accounts, Contacts, or Leads, you choose one of three actions.
| Action | New rows | Rows that match an existing record |
|---|---|---|
| Add new records | Created | Created anyway, so duplicates are possible |
| Update existing records | Ignored | Updated |
| Add new and update existing records | Created | Updated |
The first choice is the riskiest for duplicates, because the wizard does not look for matches. Use it only when you are sure the file holds records that do not exist yet, or when duplicate rules (covered below) will block the repeats.
The other two choices turn on matching. Choose "Add new and update existing records" when a file mixes both kinds of rows. Choose "Update existing records" when you want to guarantee that nothing new is created, for example when you are refreshing phone numbers from a vendor file.
Match keys the wizard offers
The match key depends on the object. The wizard shows these choices on the matching step.
| Object | Match by | Notes |
|---|---|---|
| Accounts | Salesforce ID, Name, or Site | Name and Site can be combined as a pair |
| Contacts | Salesforce ID, Name, or Email | Name matching can also use the account name |
| Leads | Salesforce ID, Name, or Email | Email is the most reliable key in most orgs |
| Custom objects | Salesforce ID or an external ID field | Name matching is not offered in the same way |
Exact option names can differ slightly between releases, so check the screen in your org. The principle is stable: the wizard compares one chosen field in your file with the same field on existing records.
How each key behaves
- Salesforce ID. The most precise key. A record ID identifies exactly one record. Use it when you exported the records first and kept the Id column.
- Name. Matches on the record name. Two different people called "Maria Lopez" can collide, and a typo in the file creates a new record instead of matching.
- Email. Matches on the email address. It is better than name for people, but it fails when the file has no email or when the org stores the address in a different format.
- External ID. A custom field that holds the identifier from another system, such as an order number or customer number. It must be marked as an External ID in the field setup. It is the best key when the source system has its own stable identifier.
Matching is a case-insensitive comparison, not a fuzzy one
The wizard matches values that are the same apart from letter case. It does not treat "Acme Corp." and "Acme Corporation" as the same company. Trailing spaces, different punctuation, and abbreviations all cause a miss. Clean these differences in the CSV before you import, using a spreadsheet's trim and find-and-replace functions.
What happens when more than one record matches
A match key is only useful if it picks out one record. If your file's value matches several existing records, the result depends on the tool and the setting, and the outcome is rarely what you want. The row may be skipped, or it may update an unexpected record. Check for non-unique keys before you import by running a report on the target object, grouped by the key field, and looking for counts above one.
The opposite problem occurs inside the file. If two rows in the CSV share the same key, the second row can update the record the first one just created. The final data then holds only the last row's values. Remove duplicate rows from the CSV first.
Duplicate rules: the safety net
Matching in the wizard is something you configure for one import. Duplicate management is a separate feature that applies to every way of creating records, including the UI, the wizard, Data Loader, and the API.
A duplicate management setup has two parts:
- A matching rule defines when two records count as duplicates. Salesforce supplies standard rules for accounts, contacts, and leads, and administrators can build custom rules with fuzzy matching on names, addresses, and phone numbers.
- A duplicate rule says what happens when a matching rule finds a duplicate. It can block the save or allow it with an alert, and it can record the event in a report.
Duplicate rules matter for imports in two ways.
- With a rule set to Block, a row that looks like an existing record fails. In Data Loader it appears in the error file with a duplicate-detected message. In the wizard it is also rejected.
- With a rule set to Allow, the row is saved. Whether the user sees an alert does not matter in an import, because nobody is watching. The duplicate is created without notice unless the rule also writes a duplicate record set report entry.
Ask your administrator which duplicate rules are active on the object before a large import. A Block rule that was built for manual entry may reject hundreds of rows in your file, and an Allow rule gives you no protection at all.
Data Loader and the duplicate rule bypass
Data Loader has a setting that lets the load run with the "Allow" action for duplicate rules, called Allow duplicate rule bypass in some versions (it may appear in the API header settings). Treat it with care. Turning it on can let duplicates through that your rules would otherwise block. Use it only when you have a reason, and confirm afterward by reviewing the new records.
Avoiding duplicates with Data Loader
Data Loader does not have a "match by email" choice. Its matching tools are different, and you pick them through the operation you run.
| Goal | Operation | Key used |
|---|---|---|
| Create only new records | Insert | None, so duplicates are possible |
| Change known records | Update | Salesforce ID in the Id column |
| Create or change in one pass | Upsert | Salesforce ID or an external ID field |
Upsert is the operation designed for this chapter's problem. It looks for a record whose key field equals the value in the row. If found, it updates that record. If not found, it inserts a new one. The key must be either the Salesforce ID or a field marked External ID.
Setting up an external ID field
To upsert by your own identifier:
- In Setup, open Object Manager and choose the object, such as Account.
- Create a custom field, for example
Customer_Number__c, of type Text or Number. - Check External ID. Also check Unique if no two records should share a value, which prevents later duplicates on this field.
- Add the field to the CSV and fill it for every row.
A sample upsert file for Accounts then looks like this:
Customer_Number__c,Name,Phone,Industry
C-1001,Acme Corp,555-0100,Manufacturing
C-1002,Globex Ltd,555-0101,TechnologyIn the Data Loader upsert wizard you select Customer_Number__c as the field used for matching. Rows with C-1001 and C-1002 update the accounts that already carry those numbers, and any new number creates an account.
Rules for upsert keys
- Every row needs a value in the key column. A blank key means the row is inserted every time, so rerunning the file creates copies.
- If you did not mark the field Unique, two existing records can hold the same value, and the upsert row for that value fails with an error that multiple records were found.
- Case sensitivity of external IDs is controlled by the field's Unique setting. Choose "Treat 'ABC' and 'abc' as different values (case sensitive)" only if your source system really distinguishes them.
Find existing IDs first
When you must update by Salesforce ID, export the IDs before you build the file. This query returns the Id and current values for the accounts you plan to change, and the saved export doubles as a backup.
SELECT Id, Name, Phone, Industry FROM Account WHERE Industry = 'Energy'Join this export to your source data in a spreadsheet using a lookup on a reliable field such as the external ID or the account name. Then use the combined file for an update. Rows that find no ID in the join need review. They are either new records or mismatches you should fix by hand.
A repeatable check for duplicates after a load
Whatever tool you use, verify the result instead of assuming it worked.
- Compare counts. Rows in the file minus rows in the error file should equal the number of records created plus updated.
- Create a report on the object grouped by your match key, and look for counts above one.
- Run the Duplicate Record Sets report if your org has duplicate rules that log matches.
- Spot check ten records from the file, including some you expected to update and some you expected to create.
Common mistakes
- Choosing "Add new records" for a file that contains existing records.
- Matching on name for people or companies with common names.
- Leaving the key column blank in a few rows.
- Importing the same file twice after a partial failure without removing the rows that already succeeded. Use the success file to see which rows were saved.
- Assuming a duplicate rule protects you when it is set to Allow.
Exercise
Your file has 2,000 contacts. About half already exist in Salesforce, and you want existing contacts updated and the rest created, without duplicates. Each contact has an email address and a customer number from your billing system. Which tool, operation, and key would you choose, and what must you set up first?
Show answer
Use Data Loader with the upsert operation, matching on the customer number. First create a custom field on Contact (for example CustomerNumber_c) marked External ID and Unique, and populate it on the existing contacts, so the key is stable and no two records can share it. Make sure every CSV row has a value in that column. The Data Import Wizard with Email as the match key would also work for a file of this size, but the email can be missing or differ in format, so the unique external ID is safer.
Exercise
A teammate reruns an insert file after half of it failed on a validation rule. What goes wrong, and how do you avoid it?
Show answer
The rows that succeeded the first time are inserted again, which creates duplicates. Avoid this by using the success and error files from the first run: fix and reload only the rows in the error file. Alternatively, switch to upsert on an external ID so that rerunning the whole file updates existing records instead of copying them.