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
Jennifer Jobson 9Jennifer Jobson 9 

How to order a related list on a visualforce template

I have a table in a visualforce template that I need ordered by date.  Here is the code currently:
<table border="0" >
<tr >
  <th>Product</th>  <th>Price</th>  <th>Quantity</th>    <th>UOM</th>    <th>Number of Pallets</th>   <th>Total Weight</th>   <th>Pickup Date</th>   <th>Comments</th>
</tr>
<apex:repeat var="cx" value="{!relatedTo.Product_Entries__r}">
<tr><td>{!cx.Product__r.Name}</td>
<td> {!cx.Price__c}</td>
<td> {!cx.Quantity__c}</td>
<td> {!cx.UoM__c}</td>
<td> {!cx.Number_of_Pallets__c}{!cx.Florisil_Number_of_Pallets__c}</td>
<td> {!cx.Total_Weight_including_pallets__c}{!cx.Florisil_Line_Item_Total_Weight__c}</td>
<td> <apex:outputText value="{0, date, MMMM d','  yyyy}">
    <apex:param value="{!cx.Pickup_Date__c}" />
</apex:outputText></td>
<td> {!cx.Comments__c}</td>
       </tr>
    </apex:repeat>                 
       </table>

And here is how it looks:
User-added imageHow do I get the list to sort by pickup date?  Thanks for any help!
Arun Kumar 1141Arun Kumar 1141

Hi Jennifer Jobson 9,
You can use order by Pickup_Date__c desc  OR order by Pickup_Date__c asc from where you are quering (add in the end of the query).
Eg:- Select id,name, createddate from Account order by createddate desc
Please mark it is best answer if this helps you.

Thanks,
Arun
 

Prateek Prasoon 25Prateek Prasoon 25
Answer :-
To order the table by date, you can use the order by clause in your SOQL query that retrieves the Product_Entries__r records.
Assuming that the Product_Entries__r relationship field is a child relationship to the parent object, you can modify your query to include an ORDER BY clause that sorts by the Pickup_Date__c field:
Text
      
    
    
    
      <apex:repeat var="cx" value="{!relatedTo.Product_Entries__r}">
<!-- modify your SOQL query to include ORDER BY Pickup_Date__c -->
<apex:repeat var="cx" value="{!relatedTo.Product_Entries__r ORDER BY Pickup_Date__c}">
<tr>
    <td>{!cx.Product__r.Name}</td>
    <td>{!cx.Price__c}</td>
    <td>{!cx.Quantity__c}</td>
    <td>{!cx.UoM__c}</td>
    <td>{!cx.Number_of_Pallets__c}{!cx.Florisil_Number_of_Pallets__c}</td>
    <td>{!cx.Total_Weight_including_pallets__c}{!cx.Florisil_Line_Item_Total_Weight__c}</td>
    <td><apex:outputText value="{0, date, MMMM d','  yyyy}">
        <apex:param value="{!cx.Pickup_Date__c}" />
    </apex:outputText></td>
    <td>{!cx.Comments__c}</td>
</tr>
</apex:repeat>

This should display the table rows in ascending order based on the Pickup_Date__c field value.

If you find my answer helpful, please mark it as the best answer. Thanks!
Jennifer Jobson 9Jennifer Jobson 9
@Prateek Prasson - I added the ORDER BY statement and I get a syntax error.  What am I doing wrong?  I am not a developer, so I apologize tor the basic question.  Thank you!
User-added image
Thanks,
Jennifer