I am trying to build a reusable VF component which can return all the child of an parent object. I just assign the parent object in my VF page and rest is taken care by the VF component to display the child records. So far I got the parent, but figuring out how to go from here to get the child records( or at least figure out how to query from the parent record that I have)
VF component:
<apex:component controller="RecordDisplayController">
<apex:attribute name="objectName" description="An Sobject" type="Sobject" required="false" assignTo="{!objectvar}"/>
<apex:pageBlock title="Related List">
<apex:pageblocktable value="{!relatedList}" var="r">
<apex:column value="{!r.name}"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:component>
Controller of VF component:
public class RecordDisplayController {
public List<Contact> relatedList{get;set;}
public sObject objectvar;
public void setobjectvar(sObject s){
objectvar=s;
relatedList=[select Name from Contact where parentid=:objectvar.id]; // Does'nt work this way!!
}
public sObject getobjectvar(){
return objectvar;
}
}
My VF page just has:
<apex:page standardController="Account">
<c:recorddisplay objectName="{!Account}" />
</apex:page>
The query to get the related list isn't correct. Also , is there a way I can get the child record of sObject objectvar;
without mentioning the child name in the query explicitly? like - relatedList=[select Name from **Child_relationship_name** where parentid=:objectvar.id];
and relatedList can accomodate any Sobject like - public List<Sobject?> relatedList{get;set;}
? Is there a way to achieve this?