You need to sign in to do that
Don't have an account?
Ossie
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]; } }
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];
All Answers
add LIMIT 100 at the end of your SOQL query:
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.
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];
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
Yes, that is the correct way.
Please mark the solution as solved! :smileyvery-happy: