Sub topic 6
Exporting records with Data Loader
Data Loader is Salesforce's desktop client for bulk data operations. For backups, you use two of its operations: Export, which writes the rows returned by a SOQL query to a CSV file, and Export All, which does the same but also includes soft-deleted and archived records. This chapter covers the interactive workflow and the decisions that determine whether the files are usable for a restore.
This chapter assumes you can write basic SOQL (covered in the previous chapter), have Data Loader installed, and have an administrator profile in a sandbox or Developer Edition org.
What Data Loader is good at
Data Loader sits between a report export and the Bulk API.
- Reports are capped at a small row count and are shaped for reading, not restoring.
- Data Loader exports an object's full row set, with 18-character Ids, as CSV.
- It runs on one machine, with one user logged in, on a manual or scripted schedule.
It does not produce a point-in-time snapshot. A large export reads rows over several minutes, so records edited during the run can appear in inconsistent states across objects. Run exports during a quiet period if consistency matters.
The export workflow
The wizard has the same steps for Export and Export All.
- Choose Export or Export All, then log in. Use the production or sandbox login option that matches your org.
- Select the object. Check Show all Salesforce objects if it is missing from the list, because the default list hides some system objects.
- Choose a target CSV file.
- Select fields, add optional
WHEREconditions, and review the generated query. - Click Finish and confirm the run.
When the run completes, Data Loader reports the number of rows written. Record that number. You will need it for verification.
Writing queries that restore well
The wizard builds a plain SELECT. Its field picker only lists fields on the chosen object, so a query that reaches into a parent has to be typed into the query box. Here is a query that covers the fields needed to rebuild an Account.
SELECT Id, Name, Industry, OwnerId, ParentId, CreatedDate, LastModifiedDate
FROM Account
ORDER BY IdOwnerId and ParentId are relationship Ids. Keep them in every export, because they are what let you reconnect records later. Chapter 12 covers restore order in detail.
Parent fields
You can traverse up to a parent with dot notation and the value lands in a CSV column.
SELECT Id, FirstName, LastName, AccountId, Account.Name
FROM ContactThe Account.Name column helps a human read the file. It is not what you load back. Load AccountId.
Child records
Data Loader writes flat CSV, so it cannot export a subquery such as (SELECT Id, Subject FROM Tasks). Export each child object with its own query and keep the lookup field that points to the parent.
SELECT Id, Subject, WhoId, WhatId, OwnerId, ActivityDate
FROM TaskIncremental filters
Filter on LastModifiedDate to export only what changed since the last run.
SELECT Id, Name, Industry, LastModifiedDate
FROM Account
WHERE LastModifiedDate >= 2026-11-01T00:00:00Z
ORDER BY LastModifiedDateAn incremental file does not tell you what was deleted. Pair it with a periodic full export.
Settings that affect the output
Open Settings before the first run and check the following.
| Setting | Why it matters for backups |
|---|---|
| Use Bulk API | Processes large exports in server-side batches. Better for objects with hundreds of thousands of rows. |
| Batch size | Rows per request. Smaller values avoid timeouts on objects with large text fields. |
| Write UTF-8 | Keeps accented and non-Latin text intact. Turn it on. |
| Time zone | Controls how date and time values are written. Pick one and keep it the same across runs. |
| Compression and timeouts | Raise the timeout if wide objects fail partway through. |
Setting names vary slightly between Data Loader versions, so check the Settings dialog in your installed version.
Fields that need attention
- Compound fields. Addresses and geolocation are compound fields. Select the component fields (
BillingStreet,BillingCity, and so on) instead. - Long text and rich text. These can contain commas and line breaks. Data Loader quotes them correctly, so the file is valid CSV, but one record can span several physical lines.
- Formula and roll-up fields. They export fine but cannot be loaded back. They are recalculated by Salesforce, so keep them for reference only.
- Fields you cannot see. Data Loader runs as the logged-in user. Field-level security hides fields from the query, and sharing rules can hide rows. Export as a user with View All Data and access to every field, or the backup will be silently incomplete.
Opening and checking the file
Opening a CSV in Excel and saving it can rewrite dates, drop leading zeros in text fields, and change line endings. Treat the file Data Loader wrote as the backup and open copies only for reading.
Because of multiline fields, counting lines with wc -l overstates the row count. Use a CSV parser.
import csv
import sys
csv.field_size_limit(sys.maxsize)
with open("accounts.csv", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
header = next(reader)
rows = sum(1 for _ in reader)
print(f"{len(header)} columns, {rows} rows")Compare the row count with SELECT COUNT() FROM Account run in the Developer Console or Workbench, and with the number Data Loader reported. If the counts differ and no one is editing the object, investigate before trusting the backup.
Common errors
- QUERY_TIMEOUT or a stalled export. The query is too heavy. Add an indexed filter, reduce the batch size, or split the export by date ranges on
CreatedDate. - Invalid field. The field does not exist on that object or the user cannot see it. Check spelling and field-level security.
- Login fails for a user who can log in to the browser. Check the login URL (production versus sandbox) and that the user's profile allows API access.
- Fewer rows than expected. Soft-deleted records are excluded by Export. Use Export All when you want them. The next chapter covers that case.
Exercise
You export Contacts with Data Loader and the CSV has 10,000 physical lines, but SELECT COUNT() FROM Contact returns 9,400. Give two likely explanations and how you would tell them apart.
Show answer
The first is that some records have multiline text fields, so physical lines exceed rows. This would normally make line counts higher than record counts, as it does here. Parse the file with a CSV reader (for example Python's csv module) and count rows instead of lines. The second is that the file includes records the count does not, such as soft-deleted rows from an Export All run, or rows created after the count was taken. Check whether you used Export All and compare Ids or CreatedDate ranges. If the parsed row count equals 9,400, the file is complete.
Exercise
Why can Data Loader not export the query SELECT Id, (SELECT Id, Subject FROM Tasks) FROM Contact as one file, and what do you do instead?
Show answer
Data Loader writes flat CSV and cannot represent nested child result sets. Export Contact and Task with separate queries, and include the lookup columns (WhoId and WhatId on Task) so the relationship can be rebuilt on restore.