+ Start a Discussion
Brandon NelsonBrandon Nelson 

Return sub query data in apex visualforce page

I have a SOQL query wrote out, but I can't (Don't know how) to access it in VisualForce. Can someone please help me with the Apex page? I can return the main columns but I don't know how to return the sub query in the columns? 

APEX CLASS CODE
public with sharing class DisplayAttachments{ public List<Account> Records {get; set;} public DisplayAttachments(){ String userId = userinfo.getUserName(); Records = [select Id,Name,(Select Id,Name from Attachments) From Account Where CreatedBy.Name = iserId]; } }
VISUALFORCE PAGE
<apex:page controller="DisplayAttachments"> <apex:pageBlock title="My Attachments"> <apex:pageBlockTable value="{!Records}" var="Record"> <apex:column > <apex:facet name="header">Client ID</apex:facet> <apex:outputText value="{!Record.ID}"/> </apex:column> <apex:column > <apex:facet name="header">Client Name</apex:facet> <apex:outputText value="{!Record.Name}"/> </apex:column> <apex:column > <apex:facet name="header">Attachment ID</apex:facet> <apex:outputText value="{!Record.ID}"/> </apex:column> <apex:column > <apex:facet name="header">Attachment Name</apex:facet> <apex:outputText value="{!Record.Name}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
 
sfdcMonkey.comsfdcMonkey.com
hi Barandon Nelson
you can do it by ussing nested page <apex:pageblockTable>
and also use createdById insted of createBy.Name
i update you code try this onces
public with sharing class DisplayAttachments{
 public List<Account> Records {get; set;}
 public DisplayAttachments(){ 
  String userId = userinfo.getUserId();
     system.debug('@Developer--> userName--' + userId);
  Records = [select Id,Name,(Select Id,Name from Attachments) From Account WHERE CreatedById =:userId ]; 
  } 
 }
 
<apex:page controller="DisplayAttachments" > 
<apex:pageBlock title="My Attachments"> 
  
   <apex:pageBlockTable value="{!Records}" var="Record">
 <apex:column >
 <apex:facet name="header">Client ID</apex:facet>
 <apex:outputText value="{!Record.Id}"/> 
 </apex:column> 
 <apex:column >
 <apex:facet name="header">Client Name</apex:facet>
 <apex:outputText value="{!Record.Name}"/>
 </apex:column> 
 <apex:column >
 <apex:facet name="header">Attachment ID</apex:facet> 
   
    <apex:pageBlockTable value="{!Record.Attachments}" var="a">
        <apex:column>
            <apex:outputField value="{!a.Id}"/>
        </apex:column>
    </apex:pageBlockTable>
        
    
 </apex:column> 
 <apex:column > 
 <apex:facet name="header">Attachment Name</apex:facet>
     <apex:pageBlockTable value="{!Record.Attachments}" var="a">
        <apex:column>
             <apex:outputField value="{!a.Name}"/>
        </apex:column>
    </apex:pageBlockTable>
 </apex:column>-->
   </apex:pageBlockTable> 
  
 </apex:pageBlock>
  
 </apex:page>
result
User-added image

Thanks
mark it best answer if it helps you so it make proper solution for others in future :)

let me inform if it helps you.


 
Brandon NelsonBrandon Nelson
Thanks Mark! Looks promising, I will try it tomorrow on my Sandbox and let you know.