So i am trying to automate some of the data tasks and Export data from an org to be imported into scratch orgs on creation.

The records I am trying to export have record types. The record types are associated with a managed package

When I perform the following to get the data out of the org (Following this example) I get an error

 sfdx force:data:tree:export -u SOURCE -q "Select Id, Name, RecordtypeId From OBJECT" --prefix export-demo --outputdir sfdx-out --plan

However I get the following error

ERROR: Reference RecordType not found for RecordTypeId. Skipping record undefined.

As expected when I remove the RecordTypeId from the query it works, but I need to have this value, preferably by name and not Id since the Ids will be different in different orgs.

So how does one automate the export and loading of data when record types exist. Is it possible to do so without editing the file every time to add the recordtypeIds?


Per sfdxfox comment, I tried with RecordType.Name and the records exported but then when I went to import into the new org I got:

INVALID_FIELD Cannot reference a foreign key field RecordType.

So I got the records but was unable to import them.

Hoping there is some way to automate this across scratch orgs or the data loading part to repopulate data in an org is useless for me at least because everything has record types....

  • 2
    Did you try RecordType.Name?
    – sfdcfox
    Commented Nov 19, 2017 at 3:31
  • @sfdcfox - I just did. It allowed me to export the records, but when I tried to import I got the "INVALID_FIELD Cannot reference a foreign key field RecordType" error.
    – Eric
    Commented Nov 19, 2017 at 4:01
  • 1
    Did you deploy the metadata first? In other words, was there a Record Type available for your object(s)? And did you grant your profile or permission set to use the record type?
    – sfdcfox
    Commented Nov 19, 2017 at 5:07
  • @sfdcfox - yup. The managed package is installed and that recordtype is available. The error seems to say that you cannot insert data referencing a foreign key. Maybe it cannot be automated
    – Eric
    Commented Nov 19, 2017 at 5:09
  • 3
    Ok, I dig some digging on the DX SF group. Thomas Dvornik (SF) commented about this issue: "Unfortunately, record types don't work in data:tree:export/import. We have a work item to support them but until then something like Greg's solution is probably your best bet." - Nov 30th
    – Adam M
    Commented Feb 8, 2018 at 17:33

1 Answer 1


I had this same issue, so I wrote a SFDX plugin to help. In the export query, you select RecordType.DeveloperName along with other fields from your object. Then run my plugin against the JSON data file and a specific scratch org. In turn it will output a version of the JSON data file with the scratch org specific Record Type Id in it. Then you can data tree import that version into your scratch org. If I get a chance, I may enhance the plugin further, but it is useful in its current state.

Here's some links for how to install and exact syntax:
Github repo: https://github.com/billryoung/sfdx-wry-plugin
Blog article walking through how to use it:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .