SFDX -How to Export data with record types to put in a scratch / qa org

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….


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:

Source : Link , Question Author : Eric , Answer Author : William Young

Leave a Comment