I have a custom object, myObject__c and on the Product2 object we have a field (Product2.myObject_lookup__c) that is a lookup to the myObject__c object. I need to export the relationship between Product2 and myObject__c so that I can import the data into a sandbox that was recently refreshed. However, I cannot export the Product2 and myObject__c records together.


  1. How can I export the records for both objects such that they create the same data in a sandbox?

What we tried but no success

sfdx force:data:tree:export -u myTargetUsername --query "select id, (select id from product2) from myObject__c" --prefix data-export mydir/targetFolder --plan

Alternative that didn't export the myObject__c records, only Product2

sfdx force:data:tree:export -u myTargetUserName --query "select id, myObject_lookup__r.Name, myObject_lookup__r.field1__c, myObject_lookup__r.field2__c from Product2" --prefix data-export mydir/targetFolder --plan

Error message

Error at Row:1:Column:28
If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
  • Did you check if user has access to this custom object and the field on product2? May 28 at 18:30

in the inner soql query , you have to use plural label appended with __r.

go to product2 object, open the lookup field with custom object. note down child relationship name. lets say child relationship name is products, then you have to use products__r in inner query.

select id, (select id from Products__r) from myObject__c

  • that is what i was missing, thanks May 28 at 19:08

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.