SFDX force:data:tree:export Beyond 1 Level Deep

How are people doing force:data:tree:export/import on data in their orgs when the object relationship runs more than 1 level deep? From the doc:

In each specified relationship, only one level of parent-to-child relationship can be specified in a query. For example, if the FROM clause specifies Account, the SELECT clause can specify only the Contact or other objects at that level. It could not specify a child object of Contact.

I’ve seen this post where the author tries to explain (in the second answer) in word how this is possible by manually splicing the plan together but I’m not quite following. If someone could post and example showing how the queries need to be run and what the final plan file should look like that would be fantastic.

Answer

Hi following the instructions in your referenced link I managed to extract and import data from 2 levels deep relationship, Account -> Cases -> Tasks & Events. This is what I did:

  1. Extract Account & Cases with its fields using the plan flag. Remember to extract all the fields you want to insert
    $ sfdx force:data:tree:export -q "SELECT Id, Name, (SELECT Id, subject, priority, origin, status FROM Cases) FROM Account" -d data --plan
    
  2. Rename the Cases.json file in order to save the references
  3. Extract the Cases & Tasks-Events using the plan flag
    $ sfdx force:data:tree:export -q "SELECT Id, (SELECT Id, subject, activitydate, priority, status FROM Tasks), (SELECT Id, subject, activitydatetime, location, DurationInMinutes FROM Events) FROM Case " -d data --plan
    
  4. Delete Cases.json file and replace it with the one saved in step 2
  5. Add the Case-Task-Event-plan.json (without the Case Part) at the end of the Account-Case-plan.json. Make sure both saveRefs and resolveRefs are marked true
  6. Import data
    $ sfdx force:data:tree:import -p data\Account-Case-plan.json
    

Hope this workaround works until Salesforce enables a way to support this.

I have opened a new issue in the salesforce-dx repository hoping to get heard https://github.com/forcedotcom/salesforcedx-vscode/issues/1204

Attribution
Source : Link , Question Author : Homerlex , Answer Author : mnunezdm

Leave a Comment