Sub topic 1
Seeding Test Data into Salesforce Scratch Orgs and Sandboxes
Prerequisites
You should understand Salesforce objects, fields, and lookup relationships. You also need the Salesforce CLI (sf) installed and an authorized source org.
Why Seed Test Data?
A new scratch org usually has metadata but little or no business data. A sandbox may contain copied production data, depending on its type and template. Seeding adds the records that developers and testers need to exercise real workflows.
Good seed data is:
- Small enough to load quickly.
- Representative of important business cases.
- Related correctly across objects.
- Safe to store outside production.
- Repeatable, so setup produces the same usable state each time.
Avoid copying more data than the test requires. A focused dataset makes failures easier to reproduce.
Scratch Orgs and Sandboxes: What Starts Empty and What Is Copied
A scratch org is temporary and is created from a definition file. It starts without your business records, so a setup process normally deploys metadata and then loads seed data.
A sandbox is a copy of a production org. Its type controls what is copied:
| Environment | Metadata copied | Production data copied |
|---|---|---|
| Scratch org | Deployed or source-tracked metadata | No |
| Developer sandbox | Yes | Limited amount |
| Full sandbox | Yes | Full copy, subject to its template |
A sandbox refresh can replace the records you created manually. Use sandbox templates and post-copy automation when data must be restored consistently.
Designing a Small Dataset That Respects Object Dependencies
Start with the records required by the workflow, then follow each relationship to its parent. For example, a Contact usually needs an Account first. Required fields, validation rules, record types, and ownership can add dependencies.
Use stable reference values while designing the dataset:
- Create parent records such as Accounts.
- Create child records such as Contacts.
- Add records for both normal and failure cases.
- Remove fields that are not needed for the test.
Exporting a Dataset from a Source Org
The tree export command writes related records to JSON files and creates a plan file. The plan preserves relationships during import.
sf data export tree \
--query "SELECT Id, Name, Industry, (SELECT Id, FirstName, LastName, Email FROM Contacts) FROM Account WHERE Industry = 'Technology' LIMIT 10" \
--plan \
--output-dir data \
--target-org my-source-orgReview the exported files before committing them. Remove sensitive values and confirm that required fields and relationships are suitable for the target org.
Exercise
Why should an Account be loaded before its related Contacts?
Show answer
The Contact records need a valid Account reference, so the parent records must exist first.