17

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.

1 Answer 1

11

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

5
  • I tried same thing, for me case reference Ids are not same in cases.json files. So my tasks are attached with wrong case. Please let me know if I am doing anything wrong.
    – Sandeep
    Commented Jan 22, 2020 at 14:56
  • I just want to tell users of Field Service Lightning, that the convention about relationships being suffixed with an __r does not always apply. This is a valid query for me: SELECT Name, ExecutionOrder, WorkOrder.Description, WorkOrder.Id, WorkOrderLineItemId, WorkPlanTemplateId FROM WorkPlan
    – bkwdesign
    Commented Mar 18, 2022 at 3:04
  • For those who see this don't miss the last piece of step 5 (saveRefs and resolveRefs being true). That caught me for a bit.
    – GeorgeA
    Commented Aug 12, 2022 at 12:49
  • How does this work if you've got a million records? There's no guarantee the child records are going to be of the right parents Commented Sep 7, 2022 at 13:16
  • sfdx force:data:tree:import/export only supports up to 200 records, if you want to process more, I would recommend to use bulk api using external ids
    – mnunezdm
    Commented Sep 8, 2022 at 8:27

You must log in to answer this question.

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