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
Derek Davis 7Derek Davis 7 

Simple Apex Search - How to define Order of Search/Results?

Hello All,

I'm sure this is something simple... I have an Apex Controller and Visualforce page that simply allows a user to search for a name of an "Asset" (custom object) and it returns "Service Request" (another custom object) that are related to that Asset. 

I am limiting the search to only return 20 results, but I would like to to return the most recent 20 records. Right now it seems to be returning the first 20 records created instead of the last (most recent) 20 records created.

This is my controller:
public class ServiceRequestSearchController {
public list <Service_Request__c> sr {get;set;}  
   public string searchstring {get;set;}  
   public ServiceRequestSearchController(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.name like \'%'+searchstring+'%\' Limit 20';
     sr= Database.query(searchquery);  
   }  
   public void clear(){  
   sr.clear();  
   } 
 }


This is my visualforce page:
<apex:page standardController="Service_Request__c" showHeader="false" extensions="ServiceRequestSearchController">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!sr}" var="s">  
     <apex:column headerValue="Service Request #" >
        <apex:outputText>{!s.Name}</apex:outputText>
     </apex:column>  
     <apex:column headerValue="SR Status" >  
      	<apex:outputText>{!s.Status__c}</apex:outputText>  
     </apex:column>   
	<apex:column headerValue="Problem Reported" >  
      	<apex:outputText>{!s.Subject__c}</apex:outputText>  
     </apex:column>     
	<apex:column headerValue="Priority" >  
      	<apex:outputText>{!s.Priority__c}</apex:outputText>  
     </apex:column>         
     <apex:column headerValue="Created Date" >  
      	<apex:outputText>{!s.SR_Created_Date__c}</apex:outputText>  
     </apex:column>           
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>


Any assistance you can provide would be greatly appreciated!! Thanks in advance!


 
Pankaj_GanwaniPankaj_Ganwani
Hi,

You will have to sort the query results by placing order by clause in query:
 
public class ServiceRequestSearchController {
public list <Service_Request__c> sr {get;set;}  
   public string searchstring {get;set;}  
   public ServiceRequestSearchController(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.name like \'%'+searchstring+'%\' order by createddate Limit 20';
     sr= Database.query(searchquery);  
   }  
   public void clear(){  
   sr.clear();  
   } 
 }

 
Derek Davis 7Derek Davis 7
Thank you @Pankaj_Ganwani - How would I go about specifying order by createddate in descending order?
Pankaj_GanwaniPankaj_Ganwani
Hi Derek,

You can refer the above mentioned code.
Derek Davis 7Derek Davis 7
Thanks again Pankaj_Ganwani ! I actually did utilize the above code, but it's still returning in order of oldest to newest. So I am seeing the oldest records first.
 
Derek Davis 7Derek Davis 7
Thank you Pankaj_Ganwani! I figured it out after reading this: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm

I just had to add DESC.

Here is my final code:
 
public class ServiceRequestSearchController {
public list <Service_Request__c> sr {get;set;}  
   public string searchstring {get;set;}  
   public ServiceRequestSearchController(ApexPages.StandardController controller) {  
   }  
   public void search(){  
       string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.name like \'%'+searchstring+'%\' order by createddate DESC Limit 20';
     sr= Database.query(searchquery);  
   }  
   public void clear(){  
   sr.clear();  
   } 
 }