简体   繁体   中英

Displaying SubQuery on VF Page

Can anyone help with this

notices =[SELECT Description__c,Id,Name__c,Notice_Date__c, (select id,Name from Attachment), FROM Notice__c];  

I have object notice with attachment

It has one attachment and I want to have out put like this on vf page

Notice Name
Notice Description
Notice Date

Attachment Name or ID associated with that notice record


My Controller

public class On_StudentNoticeController {
    public List<Notice__c> notices{get;set;}

    public On_StudentNoticeController (){
        notices=new List<Notice__c>();
    }

    public PageReference Onpageload() {
        notices = [SELECT Id,  Name__c, Description__c,Notice_Date__c,
            (SELECT Id, Name FROM Attachments) 
        FROM Notice__c 
        ORDER BY Notice_Date__c DESC NULLS FIRST];
    }
}

Screenshot of my current problem:

在此处输入图片说明

Edit #2

Looks like you're trying to iterate over empty list. Your order of execution is wrong and you have overcomplicated your life. Although I would say this should be a runtime error and not compilation one, interesting. Maybe the compiler got smarter.

Here's why your code will not work:

  1. Constructor ( On_StudentNoticeController () ) fires. It creates empty list.
  2. Page gets rendered, Visualforce will display an empty list.
  3. You're calling page action to populate the list of notices but it's too late. They "will be" there when page reloads but it'll be empty initially.

Try like that? I've literally copied code for my custom object with attachments. You should just have to change few object & field names and be good to go:

public class accswithattachments {

    public List<SF_Payable__c> accs {get; private set;}

    public accswithattachments(){
        accs = [SELECT Id, Name, LastModifiedDate, 
            (SELECT Id, Name FROM Attachments)
            FROM SF_Payable__c
            ORDER BY CreatedDate
            LIMIT 1000];
    }
}
<apex:page controller="accswithattachments" >
<ol>
    <apex:repeat value="{!accs}" var="a">
        <li><apex:outputField value="{!a.Name}" /><br/>
            <apex:outputField value="{!a.LastModifiedDate}" /><br/>

            <apex:dataList value="{!a.Attachments}" var="attachment">
               <apex:outputText value="{!attachment.Name}"/>
            </apex:dataList>
            <br/>
        </li>
    </apex:repeat>
</ol>
</apex:page>

Output (for my data of course):

在此处输入图片说明

I'm quite curious why it was giving you compilation errors. I think I know why it was complaining (the list was empty). but still...


Edit #1 to answer the comment:

Here's what works for me. It's Account attachments (because I don't have your object). It doesn't even use Apex controller.

<apex:page standardController="Account" recordSetVar="accounts">
<ol>
    <apex:repeat value="{!accounts}" var="acc">
        <li><apex:outputField value="{!acc.Name}" /><br/>
            <apex:outputField value="{!acc.Description}" /><br/>
            <apex:outputField value="{!acc.LastModifiedDate}" /><br/>

            <apex:dataList value="{!acc.Attachments}" var="attachment">
               <apex:outputText value="{!attachment.Name}"/>
            </apex:dataList>
            <br/>
        </li>
    </apex:repeat>
</ol>
</apex:page>

Original answer

Your query does not compile, I think it should be something similar to this:

SELECT Id, Name__c, Description__c,Notice_Date__c,
    (SELECT Id, Name FROM Attachments)
FROM Notice__c

Once you get it to work you should read up about Visualforce tags that loop over collections (apex:dataList, apex:dataTable, apex:pageBlockTable, apex:repeat ...). Use one of them depending on how you want your output to look like

Something like this?

<apex:repeat value="{!notices}" var="notice">
    <p>
        <apex:outputField value="{!notice.Name__c}" /><br/>
        <apex:outputField value="{!notice.Description__c}" /><br/>
        <apex:outputField value="{!notice.Notice_Date__c}" /><br/>
    </p>
    <apex:dataList value="{!notice.Attachments}" var="attachment">
        <apex:outputText value="{!attachment.Name}"/>
    </apex:dataList>
</apex:repeat>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM