You need to sign in to do that
Don't have an account?

problem in Search functionality
Hi ,
i have search code below where i can search a question with its beginning letter or if i give full question but i want to get the question even if i given any letter or word in middile of that question. please help me on this .I hope there might be wrong in like condition.Can i please know how can i solve this?
Here is the code for that
Vf page
<apex:page controller="QuesSearchCont" sidebar="false">
<apex:form >
<apex:pageMessages id="errors" />
<apex:pageBlock title="Find Me A Question & Answer!" mode="edit">
<table width="100%" border="0">
<tr>
<td width="200" valign="top">
<apex:pageBlock title="Parameters" mode="edit" id="criteria">
<script type="text/javascript">
function doSearch() {
searchServer(
document.getElementById("question").value,
document.getElementById("answer").value);
}
</script>
<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
<apex:param name="Question__c" value="" />
<apex:param name="Answer__c" value="" />
</apex:actionFunction>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="font-weight:bold;">QUESTION<br/>
<input type="text" id="question" onkeyup="doSearch();"/>
</td>
</tr>
<tr>
<td style="font-weight:bold;">ANSWER<br/>
<input type="text" id="answer" onkeyup="doSearch();"/>
</td>
</tr>
</table>
</apex:pageBlock>
</td>
<td valign="top">
<apex:pageBlock mode="edit" id="results">
<apex:pageBlockTable value="{!faq}" var="faqs">
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Question" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="question" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!faqs.Question__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Answer" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="answer" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!faqs.Answer__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</td>
</tr>
</table>
<apex:pageBlock title="Debug - SOQL" id="debug">
<apex:outputText value="{!debugSoql}" />
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
</apex:page>
Controler:public with sharing class QuesSearchCont {
// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<FAQ__c> faq {get;set;}
// the current sort direction. defaults to asc
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'Question__c'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
// init the controller and display some sample data when the page loads
public QuesSearchCont() {
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
// runs the actual query
public void runQuery() {
try {
faq = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessages(e);
}
}
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
try{
FAQ__c f=new FAQ__c();
String question = Apexpages.currentPage().getParameters().get('Question__c');
String answer = Apexpages.currentPage().getParameters().get('Answer__c');
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
if (!question.equals(''))
soql += ' and Question__c LIKE \''+String.escapeSingleQuotes(question)+'%\'';
if (!answer.equals(''))
soql += ' and Answer__c LIKE \''+String.escapeSingleQuotes(answer)+'%\'';
// if (!accountName.equals(''))
// soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';
// run the query again
}
catch(NullPointerException ne){
ApexPages.addMessages(ne);
}
runQuery();
return null;
}
}
i have search code below where i can search a question with its beginning letter or if i give full question but i want to get the question even if i given any letter or word in middile of that question. please help me on this .I hope there might be wrong in like condition.Can i please know how can i solve this?
Here is the code for that
Vf page
<apex:page controller="QuesSearchCont" sidebar="false">
<apex:form >
<apex:pageMessages id="errors" />
<apex:pageBlock title="Find Me A Question & Answer!" mode="edit">
<table width="100%" border="0">
<tr>
<td width="200" valign="top">
<apex:pageBlock title="Parameters" mode="edit" id="criteria">
<script type="text/javascript">
function doSearch() {
searchServer(
document.getElementById("question").value,
document.getElementById("answer").value);
}
</script>
<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
<apex:param name="Question__c" value="" />
<apex:param name="Answer__c" value="" />
</apex:actionFunction>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="font-weight:bold;">QUESTION<br/>
<input type="text" id="question" onkeyup="doSearch();"/>
</td>
</tr>
<tr>
<td style="font-weight:bold;">ANSWER<br/>
<input type="text" id="answer" onkeyup="doSearch();"/>
</td>
</tr>
</table>
</apex:pageBlock>
</td>
<td valign="top">
<apex:pageBlock mode="edit" id="results">
<apex:pageBlockTable value="{!faq}" var="faqs">
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Question" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="question" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!faqs.Question__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Answer" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="answer" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!faqs.Answer__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</td>
</tr>
</table>
<apex:pageBlock title="Debug - SOQL" id="debug">
<apex:outputText value="{!debugSoql}" />
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
</apex:page>
Controler:public with sharing class QuesSearchCont {
// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<FAQ__c> faq {get;set;}
// the current sort direction. defaults to asc
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'Question__c'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
// init the controller and display some sample data when the page loads
public QuesSearchCont() {
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
// runs the actual query
public void runQuery() {
try {
faq = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessages(e);
}
}
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
try{
FAQ__c f=new FAQ__c();
String question = Apexpages.currentPage().getParameters().get('Question__c');
String answer = Apexpages.currentPage().getParameters().get('Answer__c');
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
if (!question.equals(''))
soql += ' and Question__c LIKE \''+String.escapeSingleQuotes(question)+'%\'';
if (!answer.equals(''))
soql += ' and Answer__c LIKE \''+String.escapeSingleQuotes(answer)+'%\'';
// if (!accountName.equals(''))
// soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';
// run the query again
}
catch(NullPointerException ne){
ApexPages.addMessages(ne);
}
runQuery();
return null;
}
}
Put '%' in Prefix also whenver you search Question__c like \'%'+searchstring+'%\'.
so change your soql string to:
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
if (!question.equals(''))
soql += ' and Question__c LIKE \'%'+String.escapeSingleQuotes(question)+'%\'';
if (!answer.equals(''))
soql += ' and Answer__c LIKE \'%'+String.escapeSingleQuotes(answer)+'%\'';
Happy Coding!!
All Answers
Put '%' in Prefix also whenver you search Question__c like \'%'+searchstring+'%\'.
so change your soql string to:
soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
if (!question.equals(''))
soql += ' and Question__c LIKE \'%'+String.escapeSingleQuotes(question)+'%\'';
if (!answer.equals(''))
soql += ' and Answer__c LIKE \'%'+String.escapeSingleQuotes(answer)+'%\'';
Happy Coding!!
Thank u very much it solved my prob.
Great!!! My pleasure! :D
1) public List<String> selectedValue {get;set;}
public List<SelectOption> getUserType()
{
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult =CustomObject__c.getUserType__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
return options;
}
With our controller logic all complete, we can call the getUserType() method from our Visualforce page, and populate the <apex:selectList> tag:
2) <apex:selectList id="countries" value="{selectedValue}"
size="1" required="true">
<apex:selectOptions value="{!countries}"/>
</apex:selectList>
3) You will query in that Custom object when the Search button will be click with selectedValue which will return of list of records. Now rerender the form or Outputpanel.
Let me know if you didn't understand the scenario or any query. Happy Coding!!!
I am just giving you the template. Change your code with template
Controller :
public class VEERU_PageController{
public String selectedValue {get;set;}
public List<SelectOption> getUserType()
{
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Book__c.UserType__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); // Here you will get the picklist values
options.add(new SelectOption('All','All'));
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
return options;
}
public List<Book__c> search(){
System.debug('====selectedValue===='+selectedValue); // You will get the Selected Value here
List<Book__c> temp = new List<Book__c>();
String queryString;
if(selectedValue == 'ALL') queryString ='SELECT id,Question__c,Answer__c from CustomObject__c';
else if(selectedValue == 'Internal') queryString ='SELECT id,Question__c,Answer__c from CustomObject__c WHERE UserType__c = 'Internal'';
else if(selectedValue == 'External') queryString ='SELECT id,Question__c,Answer__c from CustomObject__c WHERE UserType__c = 'External'';
temp = Database.query(queryString);
return temp;
}
}
Page
<apex:page controller="VEERU_PageController">
<apex:form id="form1">
<apex:selectList id="countries" value="{!selectedValue}"
size="1" required="true">
<apex:selectOptions value="{!UserType}"/>
</apex:selectList>
<apex:commandButton value="Search" action="{!search}" rerender="form1"/>
<!-- Take table here & display your record
-->
</apex:form>
</apex:page>
Hope this will help, If you still facing any issues, let me know! Happy Coding!!
It seems all correct to me, I am also not facing the compatibility error. May be you know this that the error is occuring due to the LHS & RHS of Assignment operator is different. Check your code or Share your code.
Happy Coding!!