You need to sign in to do that
Don't have an account?
Derek 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:
This is my visualforce page:
Any assistance you can provide would be greatly appreciated!! Thanks in advance!
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!
You will have to sort the query results by placing order by clause in query:
You can refer the above mentioned code.
I just had to add DESC.
Here is my final code: