function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Btuitasi1Btuitasi1 

Dynamic "Top" List

I am building a visualforce page based on Idea standard object components. I need to display the 2 most recently submitted ideas (Title, Name, Date), 2 with the most votes (Title, Name, Date), and the top 6 contributors based on number of submissions. I'm sure there is a way to do this with SOQL, but am not too sure about the syntax.

If I understand it correctly, I plug this all in my controller, then call it from my visualforce page?

Thanks for any and all help!



Best Answer chosen by Btuitasi1
SarfarajSarfaraj
In that case this is the vf you need,
<apex:page controller="topIdeaExt">
    <apex:pageBlock title="Recent 2">
        <ul>
        <apex:repeat value="{!Recent2}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Title</label>
                        :<span id="title"> <strong>{!r.Title}</strong> Util</span>
                    </li>
                    <li>
                        <label>Date</label>
                        :<span id="date"> <strong>{!r.CreatedDate}</strong></span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
    <apex:pageBlock title="Most Voted 2">
        <ul>
        <apex:repeat value="{!MostVoted2}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Title</label>
                        :<span id="title"> <strong>{!r.Title}</strong> Util</span>
                    </li>
                    <li>
                        <label>Date</label>
                        :<span id="date"> <strong>{!r.CreatedDate}</strong></span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
    <apex:pageBlock title="Top 6 Contributors">
        <ul>
        <apex:repeat value="{!Top6Contributors}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Name</label>
                        :<span id="title"> <strong>{!r['Name']}</strong> Util</span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
</apex:page>

I took the liberty to add one nested ul. Modify/Add more fields as per your need. Note the usage of apex:repeat. You can generate any html element as per your requirement.

--Akram

All Answers

SarfarajSarfaraj
Hi

Use this,
<apex:page controller="topIdeaExt">
    <apex:pageBlock title="Recent 2">
        <apex:pageBlockTable value="{!Recent2}" var="r">
            <apex:column value="{!r.Title}"/>
            <apex:column value="{!r.CreatedDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Most Voted 2">
        <apex:pageBlockTable value="{!MostVoted2}" var="r">
            <apex:column value="{!r.Title}"/>
            <apex:column value="{!r.CreatedDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Top 6 Contributors">
        <apex:pageBlockTable value="{!Top6Contributors}" var="r">
            <apex:column value="{!r['Name']}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
public with sharing class topIdeaExt {
    public List<Idea> getRecent2()
    {
        return [SELECT Id, Title, CreatedDate FROM Idea order by createdDate desc Limit 2];
    }
    public List<Idea> getMostVoted2()
    {
        return [SELECT Id, Title, CreatedDate FROM Idea order by VoteTotal desc, CreatedDate desc Limit 2];
    }
    public List<AggregateResult> getTop6Contributors()
    {
        return [SELECT CreatedById, CreatedBy.Name Name  FROM Idea group by createdbyid, CreatedBy.Name order by count(CreatedById) desc limit 6];
    }
}



Btuitasi1Btuitasi1
Thanks for the reply! However, I am not using the Salesforce standard styles. I already have a html page already coded out. I have areas where I want the Salesforce results to appear. Here is where I would need the it to appear in the html:

<ul>
                                        <li>
                                            <label>Title</label>
                                            :<span id="title"> Stuff Util</span></li>
                                        <li>
                                            <label>Username</label>
                                            :<span id="username"> Chris Brown</span></li>
                                        <li>
                                            <label>Status</label>
                                            :<span id="status"> Active</span></li>
                                        <li>
                                            <label>Date</label>
                                            :<span id="date"> 1/19/2014</span></li>

The bolded areas are where I would like them to appear as a separate instance. This would not be in a table form.

Thanks again!
SarfarajSarfaraj
In that case this is the vf you need,
<apex:page controller="topIdeaExt">
    <apex:pageBlock title="Recent 2">
        <ul>
        <apex:repeat value="{!Recent2}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Title</label>
                        :<span id="title"> <strong>{!r.Title}</strong> Util</span>
                    </li>
                    <li>
                        <label>Date</label>
                        :<span id="date"> <strong>{!r.CreatedDate}</strong></span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
    <apex:pageBlock title="Most Voted 2">
        <ul>
        <apex:repeat value="{!MostVoted2}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Title</label>
                        :<span id="title"> <strong>{!r.Title}</strong> Util</span>
                    </li>
                    <li>
                        <label>Date</label>
                        :<span id="date"> <strong>{!r.CreatedDate}</strong></span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
    <apex:pageBlock title="Top 6 Contributors">
        <ul>
        <apex:repeat value="{!Top6Contributors}" var="r">
            <li>
                <ul>
                    <li>
                        <label>Name</label>
                        :<span id="title"> <strong>{!r['Name']}</strong> Util</span>
                    </li>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageBlock>
</apex:page>

I took the liberty to add one nested ul. Modify/Add more fields as per your need. Note the usage of apex:repeat. You can generate any html element as per your requirement.

--Akram
This was selected as the best answer
Btuitasi1Btuitasi1
Thanks for all you help so far. I've plugged in your code and it works well until I add a second record. I don't think I need the repeater component. Here is a further snippet of my html so far:

<div> 
                                    <apex:pageBlock >
                                    <img src="{!URLFOR($Resource.Cost_Center, 'images/person_image.png')}" width="75" height="75" alt="costcenter"/>
                                        <ul>
                                        <apex:repeat value="{!Recent2}" var="r">
                                        <li>
                                        <ul>                                      
                                            <li><label>Idea Title</label>
                                                :<span id="title"> {!r.Title}</span></li>
                                            <li><label>Name</label>
                                                :<span id="username"> {!r.Name__c}</span></li>
                                            <li><label>Status</label>
                                                :<span id="status"> {!r.Voting_Status__c}</span></li>
                                            <li><label>Date</label>
                                                :<span id="date"> {!r.CreatedDate}</span></li>
                                            <li><a class="btn-sm btn-data" role="button"><span id="Votes">Votes</span></a> <a class="btn-sm btn-data"  role="button"><span id="Comments">Comments</span></a></li>
                                        </ul>
                                        </li>
                                        </apex:repeat>
                                        </ul>
                                        </apex:pageBlock>
                                    </div>
                                    </div><!-- end data-info-->
                                <div class="data-info">
                                <div> 
                                <img src="{!URLFOR($Resource.Cost_Center, 'images/person_image.png')}" alt="costcenter"/>
                                    <ul>
                                        <li>
                                            <label>Idea Title</label>
                                            :<span id="title"> Card Util</span></li>
                                        <li>
                                            <label>Username</label>
                                            :<span id="username"> Chris Brown</span></li>
                                        <li>
                                            <label>Status</label>
                                            :<span id="status"> Active</span></li>
                                        <li>
                                            <label>Date</label>
                                            :<span id="date"> 1/19/2014</span></li>
                                        <li>
                                           <a class="btn-sm btn-data" role="button"><span id="Votes">Votes</span></a> <a class="btn-sm btn-data"  role="button"><span id="Comments">Comments</span></a></li>                                    </ul>
                                    </div>
                            </div><!-- end data-info--></div>
 I just want the number 1 record data to fill in the first section and the number 2 record data to fill in the second section. Everything is already pre-formatted. The first section fills out nicely, but I don't want the second record to "repeat" after it. I want it in the second section provided in the html. 

Thanks again! 

Btuitasi1Btuitasi1
I got it to work! After moving around the <apex:pageBlock> and <apex:repeat> tags, I got the data and the formatting to repeat nicely!

Thanks so much!!