Can I export fields from parent sObjects using a child object query?


Question:

I have a parent object ParentObject__c and want to retrieve all names of parent objects using a child query on ChildObject__c.

In my configuration:

  • ChildObject__c is the child object.
  • ParentObject__c is the parent.

I am trying to use this query, but it's not working:

SELECT Name, ParentObject__r.Name FROM ChildObject__c

What is wrong?


Answer:

The query format is incorrect. You need to correctly reference the parent field. The correct way to query the parent's name from a child object is by using relationship fields properly.

Here’s how you can correct and structure the queries in your export.json to extract the names of ParentObject__c records that are related to ChildObject__c records:

# Query defined for the ChidObject__c, ParentObject__c is a lookup field to the ParentObject__c object
SELECT Name, ParentObject__c FROM ChildObject__c

# Query defined for the ParentObject__c
SELECT Name FROM ParentObject__c
master=false # this is required, since we want to select all parent records which are referenced by the child records

Ensure you set master = false for the ParentObject__c object to fetch only those parent records referenced by the child records.

Last updated on 18th Apr 2024