4

I have a query and I need to do a subQuery on it, I have tryed everything but I don't find the way to make it work.

select id, 
    Name,
    parentField__c,
    (Select id, name, Childfield__c
        from ChildObject__c)

    from ParentObject__c
    where ParentObject__c = :varToFilter

The error is:

Didn't understand relationship 'ChildObject__c' in FROM part of query call. 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.

What should I do to make it work.

1 Answer 1

6

Typically, the relationship is plural, and a custom relationship will alway end in __r. Try:

(SELECT Name FROM ChildObjects__r)

You can always get the verbatim name via this Execute Anonymous script:

for (ChildRelationship relation : SObjectType.ParentObject__c.getChildRelationships())
    if (relation.getChildSObject()() == ChildObject.sObjectType)
        system.debug(relation.getRelationshipName());

Whatever this script spits out, put in place of ChildObjects__r.

3
  • 1
    A child relationship name ends in __r in most situations, but not in all situations (i.e. not for standard relationships). Consider [SELECT Id, (SELECT Id FROM Opportunities) FROM Account].
    – Derek F
    Feb 9, 2017 at 19:35
  • I personally feel the easy way to get child relationship names is via workbench.
    – javanoob
    Feb 9, 2017 at 20:29
  • It's another route. You can get the describes however you wish, but the point is that's where you need to look. Workbench requires a separate login so usually I don't go that route (especially since I normally have the Dev Console open anyway).
    – Adrian Larson
    Feb 9, 2017 at 20:30

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.