You need to sign in to do that
Don't have an account?
System.SearchException: search term must be longer than one character: *
I'm new to Apex programming and am looking for a way to catch the exception below?
Is there a better way to implement the DoSearch(); method?
Apex script unhandled exception by user/organization:
Visualforce Page: /apex/Solution_Search
caused by: System.SearchException: search term must be longer than one character: *
Class.SolutionListController.doSearch: line 43, column 35 External entry point
public with sharing class SolutionListController {
public SolutionListController(ApexPages.StandardSetController controller) {
}
Public String solutionId {get;set;}
public List<Solution> results {
get{
if(results == null) results = new List<Solution>();
return results;
}
set;
}
public ApexPages.standardcontroller Controller {get;set;}
public Solution s { get; set; }
public SolutionListController(ApexPages.StandardController stdController) {
// constructor
Controller = stdController;
this.s = (Solution)stdController.getRecord();
}
public String searchText {
get;
set{
value = value.endsWith('*') ? value : value + '*';
searchText = value;
}
}
// TODO: Create a Boolean property called "showSearch" that returns a true value instead of null
public Boolean showSearch {
get{
if (showSearch == null) showSearch = true;
return showSearch;
}
set;
}
public PageReference doSearch() {
results = (List<Solution>)[FIND :searchText IN ALL FIELDS RETURNING Solution(Id, SolutionNumber, SolutionName, Status, CreatedById, CreatedDate)] [0];
return null;
}
public PageReference selectSolution() {
Solution s = [Select Id, SolutionNumber, SolutionName, Status, CreatedById, CreatedDate from Solution where Id =:solutionId];
Controller = new ApexPages.standardController(s);
return Controller.view();
}
}
if(searchText.length() >= 3){ results = (List<Solution>)[FIND :searchText IN ALL FIELDS RETURNING Solution(Id, SolutionNumber, SolutionName, Status, CreatedById, CreatedDate)] [0];}
that should do it. essentially, you need to prequalify the text you're passing to SOSL to ensure that it won't throw that exception. in this case, it'll just end up rendering nothing until that >=3 condition is met. by practice, you want to evaluate the types of searches you want the user to be able to execute, and code based on that. for instance, your query uses the IN ALL FIELDS syntax, which can have performance lag when you execute a search with very few characters, vs. a more refined search which will be able to execute and return results faster. if milliseconds difference doesn't matter to you, than just coding to meet the mininum character requirement (3) for the SOSL query.