Skip to content

Files

Latest commit

0e86992 · Feb 18, 2021

History

History

dummy-data

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 18, 2021
Feb 9, 2021

How to use dummy data with SFDX

Exporting with record types

Always add RecordType.DeveloperName to your SOQL query when exporting, if you want to keep the record type intact. A plugin is fixing the record type ID before it is imported to the new scratch org, so you won't have to do a single thing.

Exporting data from an org

For exporting with lookups, the easiest method is to export the parent and use relationship queries. (e.g., SELECT Name, RecordType.DeveloperName, (SELECT LastName FROM Contacts) FROM Account). This command will export your query:

sfdx force:data:tree:export --query "SELECT [fields] FROM [sObject]" --outputdir dummy-data/[name] --plan

Use a name for the feature this data is suitable for, OR the name of the object.

Sometimes, you'll need to export from the child with numerous parents (many lookups).

  • For the child records
    • sfdx force:data:tree:export --query "SELECT [fields] FROM [child sObject]" --outputdir dummy-data/[name] --plan
  • For the parents
    • sfdx force:data:tree:export --query "SELECT [fields] FROM [parent sObject 1]" --outputdir dummy-data/[name] (remove --plan)
    • sfdx force:data:tree:export --query "SELECT [fields] FROM [parent sObject 2]" --outputdir dummy-data/[name] (remove --plan)
    • sfdx force:data:tree:export --query "SELECT [fields] FROM [parent sObject 3]" --outputdir dummy-data/[name] (remove --plan)

You'll then rename dummy-data/[name]/[Child sObject]s-plan.json to ONLY be plan.json (in the same folder). Then edit the file, and make sure each json file containing data is present in the plan. The plan.json file is the plan for what is imported.

[
    {
        "sobject": "[Parent sObject]",
        "saveRefs": true,
        "resolveRefs": false,
        "files": ["[Parent sObject]s.json"]
    },
    {
        "sobject": "[Child sObject]",
        "saveRefs": false,
        "resolveRefs": true,
        "files": ["[Child sObject]s.json"]
    }
]

Because the json file containing data for the parent contains a referenceId, saveRefs is set to true for the parent above and resolveRefs is true for the child, you can have lookup relationships without actual IDs.

That means you'll have to edit [Child sObject]s.json (the data for the child records) to include the lookup by having "[Parent sObject]": "@[Parent sObject]RefX" (example below). Because resolveRefs is set to true on the child sObject, the SFDX import function automatically creates the lookup.

Example myFeature-plan.json:

[
    {
        "sobject": "Account",
        "saveRefs": true,
        "resolveRefs": false,
        "files": ["myFeature-Accounts.json"]
    },
    {
        "sobject": "Case",
        "saveRefs": false,
        "resolveRefs": true,
        "files": ["myFeature-Cases.json"]
    }
]

Example myFeature-Accounts.json:

{
    "records": [
        {
            "attributes": {
                "type": "Account",
                "referenceId": "AccountRef1"
            },
            "BillingState": "Wyoming",
            "ShippingState": "Wyoming",
            "BillingCity": "Cheyenne",
            "ShippingCity": "Cheyenne",
            "Name": "Yadel"
        }
    ]
}

Example myFeature-Cases.json:

{
    "records": [
        {
            "attributes": {
                "type": "Case",
                "referenceId": "CaseRef1"
            },
            "Account": "@AccountRef1",
            "Name": "My first case"
        }
    ]
}

File and folder structure

ALWAYS have dummy-data self-contained in its own folder, both for readability and avoiding overwriting existing data. If you want ALL scratch orgs to have your dummy data, add the data as dummy-data/[name]/, which contains ONE plan.json file and one or more [sObjects].json files, which are also referenced in the plan.sjon.

The init scripts for scratch org creation automatically find these plan.sjon files and imports them for you. If you don't want auto importing, have the plan.sjon file renamed to anything else, and it won't be imported.

Importing dummy data

All data is automatically imported using the init scripts for macOS or Windows, as long as they follow the folder structures defined above. See ./scripts/mac/createScratchOrg.command and ./scripts/windows/createScratchOrg.sh.