We have a list of MyCustomObject__c which have a Master/Detail relationship with Account and we set on the Account a reference to the last of MyCustomObject__c, which we call Last_MyCustomObject__c.

When we try to export the data using `sfdx force:data:tree:export -p -q myquery.txt" the export fails saying reference MyCustomObject__c not found for Last_MyCustomObject__c. What is the right way get this data so it can be imported into a scratch org?

Ideally if we would write a query that would be like

Select ID, Name, Last_MyCustomObject__c from Account


Select ID, Name, Last_MyCustomObject__c, (Select Id, Name from MyCustomObject__c where Id = Last_MyCustomObject__c ) from Account.

Additionally, if we want to import other objects related to the same Account, how can we do that, if it will not be possible to express the export in a single query?

  • Here we are trying to export the data from production, is my scratch org configuration potentially impacting the result?
    – Edmondo
    Apr 8, 2019 at 14:50
  • oops - thoguht you were trying to import to somewhere
    – glls
    Apr 8, 2019 at 14:52

1 Answer 1


You have to start at the top of the tree to get the whole record. So if you only want the one MyCustomObject__c record that is represented by the Last_MyCustomObject__c lookup, you have to sort of recreate the behavior in your query:

Select ID, Name, Last_MyCustomObject__c, 
      (Select Id, Name from MyCustomObjects__r ORDER BY CreatedDate DESC LIMIT 1) 
  From Account
  • We did that. What about now selecting objects that relate to that account? (Some Contacts?)
    – Edmondo
    Apr 8, 2019 at 17:06

You must log in to answer this question.

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