3

I have read everything in the documentation on how to export and import data between orgs using SFDX commands.

But I am struggling to write a script which serializes all related objects. Here is the ER-diagram

enter image description here

The relationships between those objects are not serialized correctly. No matter how I structure my initial queries, if I start exporting child or parent objects with or without subqueries.

extract-data.sh

#!/bin/bash

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Name, lkp_Account__c, lkp_Case__c, lkp_Contact__c, lkp_Lead__c, lkp_Opportunity__c FROM Note__c"

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Id, Name, (SELECT Id FROM Note__r) FROM Account"

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Id, Name, (SELECT Id FROM Note__r) FROM Contact"

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Id, Name, (SELECT Id FROM Note__r) FROM Case"

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Id, Name, (SELECT Id FROM Note__r) FROM Lead"

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Id, Name, (SELECT Id FROM Note__r) FROM Opportunity"

The trailheadapps/easy-spaces-lwc repo seems to do it right but doesn't share its creation script.

2
  • Using force:data:tree:export, I presume?
    – sfdcfox
    Mar 28, 2019 at 15:23
  • Yes, export and import. Mar 28, 2019 at 15:25

1 Answer 1

5

It appears that all your objects are related together. You can use parent queries in your SOQL:

sfdx force:data:tree:export --outputdir data --plan --query \
    "SELECT Name, lkp_Account__r.Name, lkp_Case__r.Subject, lkp_Contact__r.FirstName, lkp_Contact__r.LastName, lkp_Lead__r.FirstName, lkp_Lead__r.LastName, lkp_Opportunity__r.AccountId, lkp_Opportunity__r.StageName, lkp_Opportunity__r.CloseDate, lkp_Opportunity__r.Amount FROM Note__c"

Add any additional fields as appropriate.

If you had a more complicated setup that actually involved multiple queries, you'd have to manually stitch the files together by editing the "refs" in each of the JSON files, and make sure your plan file imports records in the appropriate order. Circular dependencies are not allowed as far as I can tell.

In those cases, you'd have to use the Bulk API import option to import by External ID values.

You must log in to answer this question.

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