4

I have written the below parent to child inner query which works :

        List<master__c> masterObj = new List<master__c>([Select id, (Select ID FROM detail__r) FROM Master__c]);

Which works fine. Similarly I am writing the child to parent inner query :

        List<master__c> masterObj = new List<master__c>([select id, (select id from master__c) from detail__c]);

        system.debug(masterObj);

it is giving error ** Line: 1, Column: 49 Didn't understand relationship 'master__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.

Could you please help in identifying what is wrong with the query.

Thanks.

1 Answer 1

5

For parent field references the extra "select" keyword is not used:

List<detail__c> detailObj = [select id, master__r.Id from detail__c];

See the examples in the Using Relationship Queries documentation.

It is also not necessary to wrap the query results in a list; the query results are already a list of the correct type.

4
  • Thanks. Is it not possible to get it in the form of inner query ?
    – xyz sfdc
    Aug 23, 2015 at 9:31
  • No, the syntax is ReferenceName.FieldName but you can reference many fields that way so you can query all the fields you need. One way to think of this is that for the master-detail case you are given the freedom to include a where clause and an order by clause as there may be many detail objects that you might want to filter or order. But there will only be one master object so where and order by aren't needed.
    – Keith C
    Aug 23, 2015 at 9:34
  • List<detail__c> detailObj = [Select id from detail__c d where d.Master__c in (select id from Master__c)];
    – xyz sfdc
    Aug 24, 2015 at 3:56
  • The above also worked.
    – xyz sfdc
    Aug 24, 2015 at 3:56

You must log in to answer this question.

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