+ Start a Discussion
mukesh guptamukesh gupta 

Parent-child relationship

Hi Expert,

I need to get all students name with his class. here class is a parent and student is child. means class 12 have student "A", "B" ,"C" etc.
User-added image


Shingo YamazakiShingo Yamazaki
Dear mukesh,

I think the basic SOQL is like the following:
List<Class__c> classes = [SELECT Id, (SELECT Id FROM Students__r) FROM Class__c];

If you want to retrieve a specific Class__c, add WHERE clause.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Mukesh,
  • May I request you to please create the lookup relation in student object. please check the below screen shot for further reference.

I hope it will be helpful.

Please mark it as best answer if it resolved the issue.

Best Regards
Hi Mukesh,

What exactly you want ,Do you want to display in detail page ? if yes you can edit your class layout then add student as a related list .

If you want in apex .then you can get in two diffrent way .

Parent to child and child to parent Query .

If you want only class Name in staudent query then go for child to parent which can be solved by dot notation like below .

Check below  link it will help !!


Please check and let us know if it helps !!