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
OssieOssie 

Please Help!! - Limit rows in SOQL

Hi,

 

Can someone please tell me how i can limit the number of results returned.  ie. if more than 100 records are found than an error message showed be displayed to the user stating that you must narrow your search etc.

 

public class CaseController1 {

    public String getSearchText() {             
        return null;               
        }
    
    private List<Case> result = new List<Case>();   
    private String searchText;
    
    public CaseController1(){   }
    
   public List<Case> getResult() {return result;}

   public void setSearchText(String searchText)
        { this.searchText = searchText; }

        public void search() {
        String queryText = searchText + '%';                     
        result = [select Model_Number__c from Case Where Model_Number__c like :queryText];
}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
nduchnduch

Question 1: order the SOQL results somehow using ORDER BY and ASC or DESC command in your select query (e.g. ORDER BY field_name ASC). This way you can influence which 100 records are returned

 

Question 2: create two fields in Visualforce (see below):

 

then in your Apex have a method that will have a SELECT query something like this:

 

result = [SELECT ......... WHERE Model_Number =: input1 AND AccountName =: input2];

 

<apex:inputText id="modelNumber" value="{!input1}"/>
<apex:inputText id="accountName" value="{!input2}"/>

All Answers

nduchnduch

add LIMIT 100 at the end of your SOQL query:

 

result = [select Model_Number__c from Case Where Model_Number__c like :queryText LIMIT 100];
OssieOssie

Doh!!  Thank you.

 

If you dont mind i have 2 questions please: if there are 200 records, how will the apex class determine which 100 to display? and...

 

Any idea how i can now add another field to my visualforce page so that user can search records using 2 fields for example i want the user to enter a Model Number and Account Name.

 

Thanks in advance.

 

<apex:page id="Cas" controller="CaseController1" tabStyle="Case" > 
 
 <apex:pageBlock title="Search Queries">  
   <apex:form >   
    <apex:inputText id="searchBox" value="{!searchText}"/>   
   <apex:commandButton action="{!search}" value="Search" id="searchBtn"/>
   <apex:dataTable value="{!result}" var="cas" id="data" >    
    <apex:column >     
     <apex:facet name="header"><b>Model Number</b></apex:facet>
     {!cas.Model_Number__c}    
    </apex:column>   

   </apex:dataTable> 
  </apex:form> 
 </apex:pageBlock>
</apex:page>

 

nduchnduch

Question 1: order the SOQL results somehow using ORDER BY and ASC or DESC command in your select query (e.g. ORDER BY field_name ASC). This way you can influence which 100 records are returned

 

Question 2: create two fields in Visualforce (see below):

 

then in your Apex have a method that will have a SELECT query something like this:

 

result = [SELECT ......... WHERE Model_Number =: input1 AND AccountName =: input2];

 

<apex:inputText id="modelNumber" value="{!input1}"/>
<apex:inputText id="accountName" value="{!input2}"/>
This was selected as the best answer
OssieOssie

Thank you very much.  That works perfectly.

 

Last question pleaseeeee if i want to add more fields for users to search on is this the best method? for example  do i just keep adding to my SELECT statment

 

result = [SELECT ......... WHERE Model_Number =: input1 AND AccountName =: input2 AND Field3 =: Input3 AND Field4 =: Input4];

 

Thanks in advance

nduchnduch

Yes, that is the correct way.

 

Please mark the solution as solved! :smileyvery-happy: