+ Start a Discussion

Account history data table on visual force page



I have a visualforce page which overrides the standard account page. I'm trying to make it so that I can display the Account History related list on this page.


I found some code to do this in apex;


<apex:dataTable value="{!Account.histories}" var="accountHistory" width="100%" rows="5">
<apex:column >
<apex:facet name="header">Date</apex:facet>
<apex:outputText value="{0,date,MM/dd/yyyy HH:mm }">
<apex:param value="{!accountHistory.createddate}" />
<apex:column >
<apex:facet name="header">Field</apex:facet>
<b> <apex:outputText value="{!IF(CONTAINS(accountHistory.field,'__c'),LEFT(accountHistory.field, LEN(accountHistory.field) -3),accountHistory.field)}"/></b>
<apex:column >
<apex:facet name="header">Edited By</apex:facet>
<apex:outputText value="{!accountHistory.createdby.name}"/>
<apex:column >
<apex:facet name="header">Old Value</apex:facet>
<apex:outputText value="{!accountHistory.oldvalue}"/>
<apex:column >
<apex:facet name="header">New Value</apex:facet>
<apex:outputText value="{!accountHistory.newvalue}"/>

 However the data is not sorted, I was wondering if it possible to sort this data to put the most recent changes at the top of the list?


Any help would be appreciated!







Best Answer chosen by Admin (Salesforce Developers) 

One option would be to create a conroller extension for your page and query the AccountHistory object within the controller extension to get your fields that populate the visualforce page.  You could then sort your SOQL query by AccountHistory.CreatedDate desc.  Instead of binding to Account.histories in your data table, you could bind to the list of AccountHistory records returned in your controller extension.