Find your content:

Search form

You are here

Display List from an SOQL request with a subquery

 
Share

I'm looking for a way to display in a VisualForce page the records from a list, including the content of the subquery in it.

public class MyController {
    public List<SObject> myList {get;set;}

    /* Constructor */
    public MyController() {
        myList = [SELECT p.Name, p.Twitter__c, (SELECT a.Id FROM Attachments a LIMIT 1) FROM Power__c];
    }
}

I'm looking to get (with the subquery) the Id of the latest Attachment, get the URL of this attachment in the VF page (with {!URLFOR($Action.Attachment.Download, '#')}). Also, I would be interested to get a list of attachment information (if there is more than one attachment).

Any idea?


Attribution to: jpmonette

Possible Suggestion/Solution #1

Visualforce bindings are quite flexible and can work their way into related records retrieved via subqueries or related records. There is also good support for List and Map data types, for more info look here.

<apex:page controller="LatestAttachmentController" >
<apex:dataTable value="{!Records}" var="Record">
    <apex:column >
        <apex:facet name="header">Name</apex:facet>
        <apex:outputText value="{!Record.Name}"/>
    </apex:column>
    <apex:column >
        <apex:facet name="header">Latest Attachment Download</apex:facet>
        <apex:outputText rendered="{!IF(Record.Attachments.size > 0, TRUE, FALSE)}">
            <apex:outputLink value="{!URLFOR($Action.Attachment.Download, Record.Attachments[0].Id)}">Download</apex:outputLink>
            <apex:outputText value="(more)" rendered="{!IF(Record.Attachments.size > 1, TRUE, FALSE)}"/>
        </apex:outputText>
    </apex:column>
</apex:dataTable>
</apex:page>

The controller is much the same as yours, I use an Order By to get the latest one.

public with sharing class LatestAttachmentController
{
    public List<Test__c> Records {get; set;}

    public LatestAttachmentController()
    {
        Records = 
            [Select Name, 
                (Select Id, LastModifiedDate 
                 From Attachments 
                 Order By LastModifiedDate DESC) 
             From Test__c t];
    }
}

Enhancement to Show More Attachments

I thought I would go for the bonus part of the question and provide a means to show more attachments! The original subquery loaded not one but all Attachments (but not the file data of course) so in addition to indicating more attachments exist, you can also list them. Here is the extended code...

enter image description here

This is the extended controller to support the 'showMore' action...

public with sharing class LatestAttachmentController {

    public List<Test__c> Records { get; set; }

    public Id MoreRecordId {get; set; } 

    public Test__c MoreRecord {get; set;}

    public LatestAttachmentController()
    {
        Records =  
            [Select Name, 
                (Select Id, Name, LastModifiedDate 
                 From Attachments 
                 Order By LastModifiedDate DESC) 
             From Test__c t
             Order By Name];
    }

    public PageReference showMore()
    {
        MoreRecord = new Map<Id, Test__c>(Records).get(MoreRecordId);
        return null;
    }
}

This is the extended Visualforce page...

<apex:page controller="LatestAttachmentController" tabStyle="Test__c">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Records">
            <apex:pageBlockTable value="{!Records}" var="Record">
                <apex:column >
                    <apex:facet name="header">Name</apex:facet>
                    <apex:outputText value="{!Record.Name}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Latest Attachment Id</apex:facet>
                    <apex:outputText rendered="{!IF(Record.Attachments.size > 0, TRUE, FALSE)}">
                        <apex:outputLink value="{!URLFOR($Action.Attachment.Download, Record.Attachments[0].Id)}">Download</apex:outputLink>
                        <apex:commandLink action="{!showMore}" rendered="{!IF(Record.Attachments.size > 1, TRUE, FALSE)}" value="(more)">
                            <apex:param name="recordId" assignTo="{!MoreRecordId}" value="{!Record.Id}"/>
                        </apex:commandLink>
                    </apex:outputText>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock id="moreattachments" rendered="{!NOT(ISNULL(MoreRecord))}">
        <apex:pageBlockSection title="More Attachments - {!MoreRecord.Name}">
            <apex:pageBlockTable value="{!MoreRecord.Attachments}" var="Attachment">
                <apex:column >
                    <apex:facet name="header">Name</apex:facet>
                    <apex:outputText value="{!Attachment.Name}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Download</apex:facet>
                    <apex:outputLink value="{!URLFOR($Action.Attachment.Download, Attachment.Id)}">Download</apex:outputLink>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4363

My Block Status

My Block Content