When working with SFDX, we hit the wall using sfdx force:data:tree:export which failed when we had lookups and master-detail relationships. We had to resort to writing some custom code to ensure we would write the parent, then the child, then update the lookup on the parent object towards the child.

For other use cases, we extracted the data simply via a set of SOQL query via REST API: about ten custom objects, each with around ten fields. Can the result of those queries be piped to another rest API, on a different org, to re-import the data? In this case we do not have a lookup, but we are not able to write a single SOQL query to be used with the force:data:tree:export

  • 2
    I've written an open-source package that does multi-object extractions and loads. On GitLab here; release with some bug-fixes coming in a few days. – David Reed Apr 26 at 17:02
  • 1
    I understand this better after reading your answer. It wasn't clear to me that your use case was specifically populating a Sandbox based on an existing data model (call me obtuse). Might be a good edit to make to the question. Went ahead and added the sandbox tag. – pchittum Apr 28 at 19:18

The answer is, "it depends". In the case of a simple set of data from one object, it is pretty easy to do by using simple command line tools.

The actual way to do it depends on the OS, of course. If you're on a mac or linux, you'll use Bash or zsh most likely. In case of windows, I'd recommend powershell as the CLI doesn't play nice for Windows Subsystem for Linux.

Not being in front of my Windows machine for the moment, here is the bash version with a pseudo-SOQL-query:

> sfdx force:data:soql:query -q 'SELECT Id, Name, Field1__c, External_Id__c etc. FROM MyObject__c WHERE ...' -r csv -u org1alias | sed s/oldvalue/newvalue/ > results.csv
> sfdx force:data:bulk:upsert -f results.csv -s MyObject__c -i External_Id__c -w 10 -u org2alias

You can actually && those two commands together on one line, but that's just crazy talk, unless you've tested what you're doing and absolutely know it will work. I actually talked about this in my Dreamforce talk last year.

PowerShell has analogous functions that will do replaces like sed does in bash. However I'm not as comfy with PowerShell stuff, and don't have a Windows machine handy to hack around with this at the moment. But suffice to say, it can be done!

  • Thanks a lot, can ids be imported as well? if I am performing two queries of linked object on org1, and I proceed to import them on org2, will it work? – Edmondo1984 Apr 24 at 5:57
  • Ah, no, you're right. In that instance you'd need to use an external Id. I'll fix my example. And in the video, I'm pulling data from and importing back into the same org. – pchittum Apr 24 at 10:13
  • So Salesforce Id are auto generated for SObjects and cannot be copied over , is that right ? – Edmondo1984 Apr 24 at 10:55
  • 2
    That's correct. they are meant to be globally unique across all of Salesforce (at least all of Salesforce that is Sales Cloud, Service Cloud, and Lightning Platform). Here's a good post that talks about it: salesforce.stackexchange.com/questions/1654/… – pchittum Apr 24 at 12:45
  • Thanks, how annoying. I need to create a piece of code just to import objects with parent children relationships because I cannot export/import – Edmondo1984 Apr 24 at 12:47

The answer seems to be NO, at least outside test execution. There is a little but very useful feature of Test.loadData which is discussed here: Test.loadData() - undocumented (but useful) behavior loading relationships that allows loading data that has relationships, but this won't work to pre-fill a sandbox with meaningful data. The following approaches are possible:

  1. Create an ExternalId that you use just in the sandbox for the sake of loading test data with relationships. It is not obvious how you could keep this working with the Metadata API, while it could easily be done with SFDX since the fields can be deployed "separately".

  2. Use Metadata API to determine which relationships your objects have and will re-wire them. This SFDX plugin https://github.com/eltoroit/ETCopyData mentions explicitly "The last step is to import the data. This steps loads the data in the destination org in the correct order (parents first, children last) and keeps the IDs of those new records because their children will require the parent’s new IDs."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.