function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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


Thanks
Mueksh

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.

Forums
I hope it will be helpful.

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

Best Regards
RahulKumar
ManojjenaManojjena
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 !!

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using  

Please check and let us know if it helps !!
Thanks 
Manoj