0

I have a simple VF page displaying parent records just fine, but I cannot get the syntax perfect to display related child records. There are only 2 child records per parent records.

<apex:includeScript value="{!$Resource.UtilJS}" />
<apex:form >
<apex:pageBlock >
   <apex:pageMessages />
<apex:pageBlock >

     //  Note: All modifications made on the page will be lost if 
     //  Return button is clicked without clicking the Save button first. 

   </apex:pageBlock>
   <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}"/>
      <apex:commandButton value="Return" action="{!cancel}"/>
  </apex:pageBlockButtons>
  <apex:pageBlockTable value="{!selected}" var="a" id="table">
      <apex:column headerValue="Name">
         <apex:inputField value="{!a.name}"/>
      </apex:column>
      <apex:column headerValue="Hierarchy">
        <apex:inputField value="{!a.Type__c}"/>
     </apex:column>
     <apex:column headerValue="Status">
        <apex:inputField value="{!a.Status__c}"/>
     </apex:column>
 </apex:pageBlockTable>

6
  • this seems to be same as salesforce.stackexchange.com/questions/76861/…
    – Anshul
    May 20, 2015 at 19:52
  • 1
    Your code sample does not appear to do anything with child or parent records... Can you please explain where they are supposed to go? May 20, 2015 at 20:07
  • No as of now it just displays Parent records...I'm just trying to add the relationship to display the 2 Child records underneath each Parent record May 20, 2015 at 20:10
  • ohhh, are you trying to get them to show up in each row along with the parent records, like each row would have a sub-table of children? I'll warn you upfront, this is not simple and will likely not look as nice as you'd want it to lol That said, you should be able to accomplish this by nesting a repeat in each column to loop over the children and output some field values May 20, 2015 at 20:18
  • Yes exactly! Man, I Honestly dont know how to write a nesting repeat and I Wouldnt know where in the vf page to insert it...im a VF noob May 20, 2015 at 20:19

1 Answer 1

2

The syntax for accessing the child records (assuming they've been queried in as a sub-select from your main records) is fairly simple.
Let's assume your parent object is called Custom_Obj__c, the child records are called Child_Obj__c, and the relationship between them is called children__r. In this case, your controller should query in the main records with something like

[SELECT Id, Name, (SELECT Id, name, other_field__c from children__r) FROM Custom_Obj__c];

Then on the VF side, you'd want to use a repeat (or another table) to loop over {!a.children__r}. Each row of this iteration will be one of the Child_Obj__c records.

<apex:pageBlockTable value="{!selected}" var="a" id="table">
  <apex:column headerValue="Name">
     <apex:inputField value="{!a.name}"/>
       <apex:repeat value="{!a.children__r}" var="child" >
         <BR/><apex:inputField value="{!child.name}" />
       </apex:repeat>
  </apex:column>
  <apex:column headerValue="Hierarchy">
    <apex:inputField value="{!a.Type__c}"/>
       <apex:repeat value="{!a.children__r}" var="child" >
         <BR/><apex:inputField value="{!child.some_other_field__c}" />
       </apex:repeat>
 </apex:column>
 <apex:column headerValue="Status">
    <apex:inputField value="{!a.Status__c}"/>
 </apex:column>

1
  • Thanks for that RC! I bet that's right but I dont know how to implement it. Some background: I installed a free app and was able to edit the VF page to reference a custom object and a few fields but I Dont know how to edit the controller - appexchange.salesforce.com/… May 20, 2015 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .