+ Start a Discussion
r nareshr naresh 

how to fetch records from parent to child?

here parent is department and child is employee

vf page
-------------
<apex:page controller="depttoemp">
  <apex:form >
    <apex:pageblock title="dept to emp">
      <apex:pageBlockTable value="{!dept}" var="d">
        <apex:column value="{!d.id}"/>
        <apex:column value="{!d.name}"/>
      </apex:pageBlockTable>
    </apex:pageblock>
  </apex:form>
</apex:page>

controller
--------------
public with sharing class depttoemp {
public list<Department__c> dept{get;set;}
public list<Employee__c> emp{get;set;}
public depttoemp ()
{
string soql='select id,name,(select id,name from Employee__r)from Department__c';
dept=Database.Query(soql);
}
}
im getting this problem
====================
System.QueryException: Didn't understand relationship 'Employee__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.










Best Answer chosen by r naresh
Grazitti TeamGrazitti Team
Hi,

I guess your child relation ship name is "Employees"
So use the following query: -
string soql='select id,name,(select id,name from Employees__r)from Department__c';

And don't forget to mark this answer as best, if answer this helps you :-)
--
Regards,
Grazitti Team
Web: www.grazitti.com

All Answers

Prafull G.Prafull G.
Check the child relationship name correctly.
Navigate to child object i.e. Employee then locate the field that is reference to Department.

On the field details check the relationship name. append __r with the name.

User-added image

For example, in above image it will be Donations__r

Let us know if this helps!
Grazitti TeamGrazitti Team
Hi,

I guess your child relation ship name is "Employees"
So use the following query: -
string soql='select id,name,(select id,name from Employees__r)from Department__c';

And don't forget to mark this answer as best, if answer this helps you :-)
--
Regards,
Grazitti Team
Web: www.grazitti.com
This was selected as the best answer