Sub topic 7
Insert, update, upsert, delete, and export
This chapter covers the six operations Data Loader offers and when to use each. It assumes you have already installed Data Loader and logged in, and that you can prepare a clean CSV file. It also assumes you know what required fields and validation rules are. The earlier chapters on CSV preparation, field requirements, and setup cover those topics. Field mapping and relationships get their own chapters, so this one only touches them where an operation depends on them.
The operations at a glance
Data Loader's main window shows a button for each operation. Every operation acts on one object at a time, and every operation except export reads a CSV file.
| Operation | What it does | Needs an Id column? | Reads a CSV? |
|---|---|---|---|
| Insert | Creates new records | No | Yes |
| Update | Changes existing records | Yes, a Salesforce ID | Yes |
| Upsert | Updates a record if it matches, otherwise creates one | Salesforce ID or external ID | Yes |
| Delete | Moves records to the Recycle Bin | Yes, a Salesforce ID | Yes |
| Hard Delete | Removes records without using the Recycle Bin | Yes, a Salesforce ID | Yes |
| Export | Writes query results to a CSV | No | No, you write a SOQL query |
| Export All | Same as Export, but includes deleted and archived records | No | No, you write a SOQL query |
All of the operations follow the same wizard pattern:
- Choose the operation and log in if prompted.
- Select the object (the "Show all objects" box exposes objects that are hidden by default).
- Pick the CSV file, or for exports, pick the destination file.
- Map columns to fields.
- Choose a folder for the result files.
- Confirm and watch the progress bar.
The sections below cover what is specific to each operation.
Insert
Insert creates one new record for every row in the file. Use it when you know the records do not exist yet. The file needs a column for each required field on the object, plus any optional fields you want to fill.
This is a minimal insert file for the Account object:
Name,Phone,Industry,Website
Acme Corp,555-0100,Manufacturing,https://www.acme.example
Globex Ltd,555-0101,Technology,https://www.globex.exampleInsert does not check for duplicates. If you run the same file twice, you get two copies of each account, unless a duplicate rule in your org blocks the second run. Data Loader does not undo anything on its own. If you need to repeat a load safely, use upsert instead.
Do not include an Id column in an insert file. Salesforce generates the ID, and the success file returns it to you.
Update
Update changes fields on records that already exist. The Id column tells Salesforce which record each row refers to. The other columns hold the new values.
Id,Phone,Industry
001XXXXXXXXXXXXXXX,555-0199,Energy
001YYYYYYYYYYYYYYY,555-0198,RetailThe IDs above are placeholders. Replace them with real IDs from an export. Update has several behaviors that surprise people:
- Only mapped columns change. If your file has
IdandPhone, then only Phone is written. Other fields are left alone, so you do not need to include every field. - Blank cells are ignored by default. An empty cell in a column you mapped does not erase the field. To clear values on purpose, enable the Settings option "Insert null values" (the exact label can differ by version), or use the value
#N/Awhere your version supports it. Check the behavior on a few test rows first. - Required fields do not have to be in the file. They only matter if you are trying to set them to blank. Validation rules still run on the changed record, though, so a rule that looks at other fields can still reject the row.
- An unknown or malformed ID produces an error row. It does not stop the whole job.
Using an export as the source
The easiest way to build an update file is to export first. A query like this returns the IDs and the current values you plan to change:
SELECT Id, Name, Phone, Industry FROM Account WHERE Industry = 'Energy'Save the export. It serves two purposes. You can edit a copy for the update, and you keep the untouched original as a backup you can load back if the update goes wrong.
Upsert
Upsert combines insert and update in one pass. For each row, Salesforce looks for a record with a matching key. If it finds one, it updates it. If not, it inserts a new one. This makes upsert the safest choice for loads you may repeat, such as a nightly sync from another system.
The matching key can be one of two things:
- The Salesforce
Idfield. Rows with an ID that exists are updated. Rows where the ID cell is blank are inserted. A row with an ID that does not exist in the org produces an error. - An external ID field. This is a custom field on the object that you marked as "External ID" (and often "Unique"). The field holds the key from your source system, such as a customer number. You choose this field on the upsert wizard's second screen.
Here is an upsert file keyed on a hypothetical external ID field named Customer_Number__c:
Customer_Number__c,Name,Phone
C-1001,Acme Corp,555-0100
C-1002,Globex Ltd,555-0101
C-1003,Initech,555-0102Rows C-1001 and C-1002 update the existing accounts that carry those numbers. C-1003 is new, so Salesforce inserts it.
Two failure modes are worth knowing:
- Duplicate keys in the file. If the same external ID value appears twice in one batch, the batch can fail or the second row can overwrite the first. Remove duplicates before loading.
- Duplicate keys in the org. If the external ID field is not unique and two existing records share a value, Salesforce cannot choose which one to update. The row fails with a "multiple matches" style error. Mark the field "Unique" to prevent this.
External IDs also let you link child records to parents without looking up Salesforce IDs. That use is covered in a later chapter.
Delete and Hard Delete
Delete needs only an Id column. Any other columns in the file are ignored for this operation.
Id
001XXXXXXXXXXXXXXX
001YYYYYYYYYYYYYYYA standard Delete moves records to the Recycle Bin. You can restore them from there, for the retention period your org uses. Deleting a parent record can also delete its children, depending on the relationship type. Deleting an Account, for example, deletes its Contacts and Opportunities. Check what is related before you delete.
Hard Delete skips the Recycle Bin and removes the records permanently. It has extra requirements:
- It uses the Bulk API, so you must enable "Use Bulk API" in Settings.
- Your user needs the "Bulk API Hard Delete" permission, which is not given to any profile by default. An administrator must assign it, usually through a permission set.
Export and Export All
Export reads data out of Salesforce. Instead of choosing a CSV to read, you choose a file to write. The wizard then shows a query builder:
- Pick the fields you want from a list.
- Optionally add
WHEREconditions in the builder. - Review the generated SOQL, which you can edit by hand.
The final screen shows the query that will run. You can paste in your own SOQL, such as the Industry query shown earlier, and this is often faster than using the builder.
Export returns only live records. Export All also returns records in the Recycle Bin and archived activities. You can use it to find records that were deleted by mistake and recover their IDs and values. It is also how you find soft-deleted rows when a count does not match what you expect.
Some points to remember about exports:
- Always include
Idif you plan to reuse the file for an update or delete. - Relationship fields can be queried through dot notation, for example
Account.Nameon a Contact query. These columns are read-only and cannot be mapped back in an update. - Compound fields such as
BillingAddresscannot be exported as one column. Query the parts, such asBillingStreetandBillingCity. - Encoding. A file opened and re-saved in a spreadsheet can lose leading zeros or change date formats. If you edit an export in Excel, check those columns before loading it back.
Choosing the right operation
Most mistakes come from picking the wrong operation rather than from wrong data. Use these checks:
- If you are not sure whether rows already exist, use upsert with an external ID. Insert may create duplicates, and update will reject rows without IDs.
- If you have IDs but want to avoid creating records by accident, use update. An unknown ID gives an error row, not a new record.
- If you want records gone but might need them back, use Delete, not Hard Delete.
- If the job changes data, export a backup first.
Exercises
Exercise
You receive a nightly file of customers from an outside system. Some already exist in Salesforce and some are new. The file has the source system's customer number but no Salesforce IDs. Which Data Loader operation fits, and what must exist on the Account object for it to work?
Show answer
Use upsert. The Account object needs a custom field marked as an External ID (preferably also Unique) that holds the customer number. In the upsert wizard, select that field as the matching key. Existing customers are updated and new ones are inserted.
Exercise
You run an update with a file that has the columns Id, Phone, and Industry. Afterwards, the Website values on those accounts are unchanged. Is this a problem? Why or why not?
Show answer
It is expected. Update changes only the fields that are mapped from the file. Website was not in the file, so it was not touched. Blank cells in mapped columns are also ignored by default, unless you enable the setting that writes null values.
Exercise
A colleague wants to permanently remove 2,000 test contacts using Hard Delete, but the button is missing or the job fails with a permission error. Name two things to check.
Show answer
First, check that "Use Bulk API" is enabled in Settings, because Hard Delete runs only through the Bulk API. Second, check that the user has the "Bulk API Hard Delete" permission, which an administrator must assign, usually through a permission set.