0

I need to query all the related children of an object, the connection is via a lookup relationship.

Query as follows to illustrate:

SELECT (SELECT Id FROM Orders) 
FROM Invoice__c

Error message:

sObject type 'Invoice__r' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

What am I doing wrong?

5
  • 4
    Using Relationship Queries
    – glls
    Jul 2, 2018 at 13:46
  • I think that the parent-to-child query only works with a master relationship not with a regular lookup relationship :( Jul 2, 2018 at 14:19
  • That is not correct, @Cubancoffee. You can perform parent-child SOQL with both master-detail and lookup relationships. You need to look at the lookup field between these objects to get the relationship name and use that name with __r in your child subquery.
    – David Reed
    Jul 2, 2018 at 14:30
  • I also added the __r but I still have the same issue. It only works for master-detail relationships :(. I know is asking a lot but, Can you maybe add an answer with your solution? just to make sure I am using the same query as you suggested. Jul 2, 2018 at 14:36
  • Are you sure you are using the correct relationship name? Can you post a screenshot of the lookup field in edit mode so the relationship name field is displayed? Jul 2, 2018 at 16:44

1 Answer 1

4

You should at least query a field from Invoice__c parent object, like below:

SELECT Id, (SELECT Id FROM Orders__r)   //check the proper API name of the child Order object 
FROM Invoice__c

Check the article from Sfdc docs for better understanding.

3
  • Since that's a custom relationship, there's going to be an __r involved.
    – David Reed
    Jul 2, 2018 at 14:06
  • I tried it but it still does not work. Error message as follows: Didn't understand relationship 'Orders' 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. Jul 2, 2018 at 14:14
  • @DavidReed yeah, that's right. I'm updating my answer :) Jul 2, 2018 at 14:44

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