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

Sorting Visualforce page by values not on object

Hello - I am very new to Visualforce and Apex dev and have run into a roadblock.  There are two objects here, Interactions and Contacts.  Interactions is a related list on Contacts.  I have been given the task to populate the most current Interaction Date on a VF table made up of Contacts and make that column sortable.  Using a function in the controller I loop through all of the interactions and set the date for the most current interaction.  I have successfully added the most current date to the table, however the sort is causing a problem.  We are currently using the following sort function.



PageReference sortContacts() {


if (sortExpression == 'Reset') {

sortExpression =


sortDirection =

'desc nulls last';



String queryString = (

'Select Id, Account.Id, Account.Name, Name, AccountSite2__c, Phone, Read__c, Account.Total_Open_Opportunity_Dollars__c, '+


'createdDate, Account_Contact_Score__c, Account_Contact_Score_Numeric__c, Last_Interaction_Date__c, InteractionScoreFormula__c, Interaction_Score_Numeric__c,Workspace_Sort__c, Action__c '+


'from Contact where AW_Contact_Status__c = \'Active\' and Account.OwnerId = \'' + UserInfo.getUserId() + '\' and Account.Is_Trade_Account__c = \'True\' and OwnerId = \''

+ UserInfo.getUserId() +

'\' and Name != \'General Contact\' and (Hide_From_Workspace_Until__c = null or Hide_From_Workspace_Until__c <= ' + dateString + ') '+


'order by ' + sortExpression + ' '+ sortDirection);



contactsCon =



// sets the number of records in each page set







As you can see we are sorting on the available fields on the Contact Object.  I need to sort on a field where I established the value (date) in the controller.  This field does not exist on the Contact Object or any other object.


I thought about creating new hidden fields on the Contact Object and trying to write (upsert?) the calculated value from the Controller.  However, I don't know that this would be advisable.


Any help that anyone could provide would be greatly appreciated.  Thanks!!