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
Jim MottolaJim Mottola 

How can I sort list by field in a Visual force page using standard Opportunity Controller?

Muddling my way through my first visual force page; not a developer by trade.  I've created a visual force button that will output the content of a related list to my opportunities (Visit Report) to a PDF file so that people can easily scroll through all VR's associated with an Opportunity & share with non-SF users.  My code is listed below.  

I have everything working as I like except the following 2 elements which I'm not sure how to handle (hoping for some help):

1) I want to sort the output Descending by the report date field
2) I want to make the headers of the table appear at the top of each page (repeating headers)

Can anybody help with this?  It seems simple but I'm getting lost in the details.
 
<apex:page standardController="Opportunity" renderAs="PDF">
<head>
<style>
@page { size:landscape;}
.headerRow .TableTitle {
            background-color: #139cd8 !important;
            cellPadding="5";}
            body {font-family: 'Arial Unicode MS'; font-size:12px}


 </style></head>
 <apex:image id="logo1" url="/servlet/servlet.ImageServer?id=015i0000002XNDw&oid=00D630000009OtH&lastMod=1394633629000" width="25%" height="25%"/>
 <br/>
 <apex:pageBlock >

        <apex:pageBlockTable value="{!Opportunity.Visit_reports__r}" var="item" cellpadding="5" columnsWidth="5%,5%,5%,10%,65%" border="1">
            <apex:column style="vertical-align:top; text-align:center" headerValue="Name" headerClass="TableTitle" value="{!item.Name}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Account" headerClass="TableTitle" value="{!item.Account__c}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Report Date" headerClass="TableTitle" value="{!item.Report_Date__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Purpose of Visit" headerClass="TableTitle" value="{!item.Purpose_of_Visit__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Visit Details" headerClass="TableTitle" value="{!item.Visit_Details__c}"/>
        </apex:pageBlockTable> 
        </apex:pageBlock>
</apex:page>

 
Best Answer chosen by Jim Mottola
karthikeyan perumalkarthikeyan perumal
Hello, 

The only way I know to do this is with a controller extension.
create a apex class like below , before saving  validate all field name and  query part, 
 
public with sharing class OppExtension
{
    private final Id OppId;
    public List<Visit_reports__c> lstVR { get; set; }

    public OppExtension(ApexPages.StandardController stdController)
    {
        OppId = stdController.getId();
        lstVR = [select Id
                              ,Name
                              ,Account__c
                              ,Report_Date__c
                              ,Purpose_of_Visit__c
			                  ,Visit_Details__c 
                     from Visit_reports__c
                   where OppId =:OppId
               order by Report_Date__c DESC];
    }
}

modify your apex pages like below, 
 
<apex:page standardController="Opportunity" extensions="OppExtension" renderAs="PDF">
<head>
<style>
@page { size:landscape;}
.headerRow .TableTitle {
            background-color: #139cd8 !important;
            cellPadding="5";}
            body {font-family: 'Arial Unicode MS'; font-size:12px}


 </style></head>
 <apex:image id="logo1" url="/servlet/servlet.ImageServer?id=015i0000002XNDw&oid=00D630000009OtH&lastMod=1394633629000" width="25%" height="25%"/>
 <br/>
 <apex:pageBlock >

        <apex:pageBlockTable value="{!lstVR}" var="item" cellpadding="5" columnsWidth="5%,5%,5%,10%,65%" border="1">
            <apex:column style="vertical-align:top; text-align:center" headerValue="Name" headerClass="TableTitle" value="{!item.Name}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Account" headerClass="TableTitle" value="{!item.Account__c}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Report Date" headerClass="TableTitle" value="{!item.Report_Date__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Purpose of Visit" headerClass="TableTitle" value="{!item.Purpose_of_Visit__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Visit Details" headerClass="TableTitle" value="{!item.Visit_Details__c}"/>
        </apex:pageBlockTable> 
        </apex:pageBlock>
</apex:page>

Hope this code will help you to fix your issue. 
make it solved if its works for you. 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

The only way I know to do this is with a controller extension.
create a apex class like below , before saving  validate all field name and  query part, 
 
public with sharing class OppExtension
{
    private final Id OppId;
    public List<Visit_reports__c> lstVR { get; set; }

    public OppExtension(ApexPages.StandardController stdController)
    {
        OppId = stdController.getId();
        lstVR = [select Id
                              ,Name
                              ,Account__c
                              ,Report_Date__c
                              ,Purpose_of_Visit__c
			                  ,Visit_Details__c 
                     from Visit_reports__c
                   where OppId =:OppId
               order by Report_Date__c DESC];
    }
}

modify your apex pages like below, 
 
<apex:page standardController="Opportunity" extensions="OppExtension" renderAs="PDF">
<head>
<style>
@page { size:landscape;}
.headerRow .TableTitle {
            background-color: #139cd8 !important;
            cellPadding="5";}
            body {font-family: 'Arial Unicode MS'; font-size:12px}


 </style></head>
 <apex:image id="logo1" url="/servlet/servlet.ImageServer?id=015i0000002XNDw&oid=00D630000009OtH&lastMod=1394633629000" width="25%" height="25%"/>
 <br/>
 <apex:pageBlock >

        <apex:pageBlockTable value="{!lstVR}" var="item" cellpadding="5" columnsWidth="5%,5%,5%,10%,65%" border="1">
            <apex:column style="vertical-align:top; text-align:center" headerValue="Name" headerClass="TableTitle" value="{!item.Name}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Account" headerClass="TableTitle" value="{!item.Account__c}"/>
            <apex:column style="vertical-align:top; text-align:center" headerValue="Report Date" headerClass="TableTitle" value="{!item.Report_Date__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Purpose of Visit" headerClass="TableTitle" value="{!item.Purpose_of_Visit__c}"/>
            <apex:column style="vertical-align:top; text-align:left" headerValue="Visit Details" headerClass="TableTitle" value="{!item.Visit_Details__c}"/>
        </apex:pageBlockTable> 
        </apex:pageBlock>
</apex:page>

Hope this code will help you to fix your issue. 
make it solved if its works for you. 

Thanks
karthik
 
This was selected as the best answer
Diana BarnesDiana Barnes
Thanks Karthik!  That worked for getting the table to be sorted by the Report Date descending after updating the variables.  Is there an easy way to get the table headers to repeat so that they are at the top of each page?