+ Start a Discussion
Ketan Solanki29Ketan Solanki29 

Parent Child Nested Query in Controller

I have two object(Parent-Child)

 

Parent Object==>> Course__c

 

Field Label Data Type

Name                             Text(80)

NoOfSubject                  Number(3, 0)
Type                                Picklist

Child Object==>> Subject__c

 

Field Label Data Type

Name                             Text(80)

Course                           Master-Detail(Course)
Marks                              Number(3, 0)
Description                    Text(255)

 

Now , I need to write a nested query like below but didn't success.

 

List<Course__c> courseList;

 

courseList = [ SELECT Id,Name,Number_Of_Subjects__c,Type__c,( SELECT Id,Name,Marks__c,Type__c FROM Course__r.Subject__c ) FROM Course__c ];

 

Error:

 

 Compile Error: Didn't understand relationship 'Course__r' in field path. 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. at line 7 column 22


Please, help me to write SOQL query for this parent child relationship. 

 

logontokartiklogontokartik

Your query looks wrong when selecting the child records. Try below

 

List<Course__c> courseList;

 

courseList = [ SELECT Id,Name,Number_Of_Subjects__c,Type__c,( SELECT Id,Name,Marks__c,Type__c FROM Subjects__r ) FROM Course__c ];

 

Ketan Solanki29Ketan Solanki29

Still i am getting this below error

 

Error: Compile Error: Didn't understand relationship 'Subjects__r' 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. at line 8 column 22

logontokartiklogontokartik

Can you click on the Subject__c's field that is referencing the Course__c object and see what child relationship name is, and you need to append __r to that name and use it in query.

 

https://skydrive.live.com/redir?resid=A80E9F4981774D08!317

 

 

 

dphilldphill

In Eclipse, open up your Schema.

 

Find the Course__c object and expand. -> Find Child Relationships and expand -> Select Subject and expand -> Expand the fields -> Click the check box on one of those fields.  It will show you how to query for the child relationship from the parent in the query builder here.