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
Lukasz PiziakLukasz Piziak 

Default sorting records in pageBlock Table by date field

Hi,

I'm looking for solution of having as default sorting my records in VF column by date field {!item.SCMC__Planned_Completion_Date__c} in ascending order. i will appreciate any help. Thanks. 

VF page
<apex:pageBlock rendered="True" title="Production Orders">
<apex:pageBlockSection title="Production Orders to Fill">
<apex:pageBlockTable value="{!objlist}" style="width:1220px" var="item">
<apex:column style="width:100px" headerValue="Production Order No.">
<apex:outputLink value="/{!item.id}" target="_blank">
{!item.Name}
</apex:outputLink>
</apex:column>
<apex:column style="width:100px" value="{!item.Sales_Order_No__c}"/>
<apex:column style="width:100px" value="{!item.Customer__c}" headerValue="Customer Name"/>
<apex:column style="width:200px" value="{!item.Assembly_Name__c}" headerValue="Hose Description"/>
<apex:column style="width:100px" value="{!item.SCMC__Start_Date__c}" headerValue="Production Start Date"/>
<apex:column style="width:100px" value="{!item.SCMC__Planned_Completion_Date__c}" headerValue="Planned Completion Date"/>
<apex:column style="width:100px" value="{!item.SCMC__Production_Status__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>

Controller

public class TestExtension {
    public List<SCMC__Production_Order__c> objlist{get;set;}
    public TestExtension(ApexPages.StandardSetController controller) {
    objlist = [SELECT Name, Sales_Order_No__c,Customer__c, Assembly_Name__c, SCMC__Start_Date__c, SCMC__Planned_Completion_Date__c, SCMC__Production_Status__c
FROM SCMC__Production_Order__c Where SCMC__Production_Status__c = 'Pending Pulling All Items'];
    }
}
Best Answer chosen by Lukasz Piziak
KaranrajKaranraj
Lukasz  - Add ORDER BY in your soql query to return the result in ascending or descending order.
 
objlist = [SELECT Name, Sales_Order_No__c,Customer__c, Assembly_Name__c, SCMC__Start_Date__c, SCMC__Planned_Completion_Date__c, SCMC__Production_Status__c
FROM SCMC__Production_Order__c Where SCMC__Production_Status__c = 'Pending Pulling All Items' ORDER by SCMC__Planned_Completion_Date__c DESC ];

 

All Answers

KaranrajKaranraj
Lukasz  - Add ORDER BY in your soql query to return the result in ascending or descending order.
 
objlist = [SELECT Name, Sales_Order_No__c,Customer__c, Assembly_Name__c, SCMC__Start_Date__c, SCMC__Planned_Completion_Date__c, SCMC__Production_Status__c
FROM SCMC__Production_Order__c Where SCMC__Production_Status__c = 'Pending Pulling All Items' ORDER by SCMC__Planned_Completion_Date__c DESC ];

 
This was selected as the best answer
Tejpal KumawatTejpal Kumawat
Hello Lukasz,

Replace your query with this snippt.
objlist = [SELECT Name, Sales_Order_No__c,Customer__c, Assembly_Name__c, SCMC__Start_Date__c, SCMC__Planned_Completion_Date__c, SCMC__Production_Status__c
FROM SCMC__Production_Order__c Where SCMC__Production_Status__c = 'Pending Pulling All Items' order by SCMC__Planned_Completion_Date__c ASC];
If this answers your question mark Best Answer it as solution and then hit Like!
 
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Lukasz, 

Please try the below updated query,

[SELECT Name, Sales_Order_No__c,Customer__c, Assembly_Name__c, SCMC__Start_Date__c, SCMC__Planned_Completion_Date__c, SCMC__Production_Status__c
FROM SCMC__Production_Order__c Where SCMC__Production_Status__c = 'Pending Pulling All Items' ORDER BY SCMC__Planned_Completion_Date__c ASC]


Thanks,
Vinoth



 
Lukasz PiziakLukasz Piziak
Thanks guys. working perfectly
san5san5
Hi , Is there any way to sort pageblocktable without controller, because I have a requirement where data in pageblock table coming from other  system.
I want to sort table by date field in ascending order .
Any help is appreciated.