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
BenBurbridge(JP)BenBurbridge(JP) 

Multiple Search Controller

Hi,

I have a controller which I have used from a glorious Bob Buzzard blog:
 
public class CSTHomeController 
{
 public String nameQuery {get; set;}
 public List<Complaints__c> com {get; set;}
  
 public PageReference executeSearch()
 {
  String queryStr='%' + nameQuery + '%';
  com=[select id, 
              Name, 
              Patient_Name_Reference__c,
              Date_and_time_complaint_lodged__c,
              Complainant_Type__c 
            from Complaints__c 
            where Patient_Name_Reference__c like :queryStr];
            
  return null;
 }
  
 public CSTHomeController ()
 {
  // if query appears in URL, execute it
  String urlQuery=ApexPages.currentPage().getParameters().get('query');
   
  if ( (null!=urlQuery) && (0!=urlQuery.length()) )
  {
   nameQuery=urlQuery;
   executeSearch();
  }
 }
}

This works perfectly, but I want to use several of these searches together on one page.  The above is for a complaints object, but we have a customer feedback object and an audits object which I want to use this for.  I am building a visualforce page which will become a Customer Service Team landing page and they can search across these different objects but ....

How do I add more searches onto the same controller?

Visualforce Page with duplicated search in very early stages of development:
 
<apex:page controller="CSTHomeController">
   <style type="text/css">
   
h1 { 
    display: block;
    font-size: 18pt;
    font-family: arial;
    font-weight: bold;
    text-align: center;
}

</style>
<h1> Customer Service Team </h1>
    <apex:form >
        <apex:pageBlock >
            <table>
                <tr>
                    <td><apex:outputlink value="https://cs21.salesforce.com/a0C/o">Complaints Home</apex:outputlink></td>
                </tr>
                <tr>
                    <td style="height:10px;"></td>
                </tr>
                <tr>
                    <td><apex:outputLabel value="Search for a Complaint"/></td>
                </tr>
                <tr>
                    <td><apex:inputText value="{!nameQuery}"/></td>
                </tr>
                <tr>
                    <td style="height:10px;"></td>
                </tr>
                <tr>
                    <td><apex:commandButton action="{!executeSearch}" value="Search"/></td>
                </tr>
            </table>  
            <apex:pageBlockTable value="{!com}" var="comp">
                <apex:column headerValue="Complaint Results">
                    <apex:outputLink value="/{!comp.id}/e?retURL={!URLENCODE('/apex/RetUrlSearchPage?query='+nameQuery)}">{!comp.Name}</apex:outputLink>
                </apex:column>
                <apex:column value="{!comp.Patient_Name_Reference__c}"/>
                <apex:column value="{!comp.Date_and_time_complaint_lodged__c}"/>
                <apex:column value="{!comp.Complainant_Type__c }"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <apex:form >
    <apex:pageBlock >
            <table>
                <tr>
                    <td><apex:outputlink value="https://cs21.salesforce.com/a0C/o">Complaints Home</apex:outputlink></td>
                </tr>
                <tr>
                    <td style="height:10px;"></td>
                </tr>
                <tr>
                    <td><apex:outputLabel value="Search for a Complaint"/></td>
                </tr>
                <tr>
                    <td><apex:inputText value="{!nameQuery}"/></td>
                </tr>
                <tr>
                    <td style="height:10px;"></td>
                </tr>
                <tr>
                    <td><apex:commandButton action="{!executeSearch}" value="Search"/></td>
                </tr>
            </table>  
            <apex:pageBlockTable value="{!com}" var="comp">
                <apex:column headerValue="Complaint Results">
                    <apex:outputLink value="/{!comp.id}/e?retURL={!URLENCODE('/apex/RetUrlSearchPage?query='+nameQuery)}">{!comp.Name}</apex:outputLink>
                </apex:column>
                <apex:column value="{!comp.Patient_Name_Reference__c}"/>
                <apex:column value="{!comp.Date_and_time_complaint_lodged__c}"/>
                <apex:column value="{!comp.Complainant_Type__c }"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        </apex:form>
</apex:page>



Thanks
Best Answer chosen by BenBurbridge(JP)
Wizno @ ConfigeroWizno @ Configero
This Salesforce doc should help: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm (http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm" target="_blank)

Your executeSearch method will change a bit though to accomdate muliple items, I actually found a blog post that illustrates what I was about to type up... Gotta love the community!

Check this out: http://www.sfdcpoint.com/salesforce/sosl-example-in-salesforce/ (The code from the page is below)

Page example
<apex:page controller="SOSLController">
  <apex:form >
  <apex:inputText value="{!searchStr}"/>
    <apex:commandButton value="Search in Account, Contact, Opportunity" action="{!soslDemo_method}"                reRender="acct,error,oppt,cont" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
                <apex:facet name="start" >
                    <img src="/img/loading.gif"/>                    
                </apex:facet>
    </apex:actionStatus>
  </apex:form>
 
    <apex:outputPanel title="" id="error">
     <apex:pageMessages ></apex:pageMessages>
     </apex:outputPanel>
 
    <apex:pageBlock title="Accounts" id="acct">
    <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 
 <apex:pageBlock title="Contacts" id="cont">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  
 <apex:pageBlock title="Opportunities" id="oppt">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
   
</apex:page>

Controller:
 
Public with sharing class SOSLController{
 Public List<Opportunity> optyList {get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
  
 Public String searchStr{get;set;}
   Public SOSLController(){
   }
  
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   conList = New List<contact>();
   accList = New List<account>();
   if(searchStr.length() > 1){
   String searchStr1 = '*'+searchStr+'*';
   String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName)';
   List<List <sObject>> searchList = search.query(searchQuery);
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   if(accList.size() == 0 && conList.size() == 0 && optyList.size() == 0){
       apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..'));
       return;
   }
   }
   else{
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..'));
   return;
   }
  }
}

 

All Answers

Gaurav NirwalGaurav Nirwal
<apex:page controller="theController">
   <apex:form>
      <apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection>
            <apex:pageBlockSectionItem>
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup>
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}" 
                                      rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l" 
                               rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.name}"/>
              <apex:column value="{!l.email}"/>
              <apex:column value="{!l.phone}"/>
           </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
</apex:page>
 
public class theController {

    String searchText;
    List<Lead> results;

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String s) {
        searchText = s;
    }

    public List<Lead> getResults() {
        return results;
    }

    public PageReference doSearch() {
        results = (List<Lead>)[FIND :searchText RETURNING Lead(Name, Email, Phone)][0];
        return null;
    }
}
Wizno @ ConfigeroWizno @ Configero
This Salesforce doc should help: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm (http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm" target="_blank)

Your executeSearch method will change a bit though to accomdate muliple items, I actually found a blog post that illustrates what I was about to type up... Gotta love the community!

Check this out: http://www.sfdcpoint.com/salesforce/sosl-example-in-salesforce/ (The code from the page is below)

Page example
<apex:page controller="SOSLController">
  <apex:form >
  <apex:inputText value="{!searchStr}"/>
    <apex:commandButton value="Search in Account, Contact, Opportunity" action="{!soslDemo_method}"                reRender="acct,error,oppt,cont" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
                <apex:facet name="start" >
                    <img src="/img/loading.gif"/>                    
                </apex:facet>
    </apex:actionStatus>
  </apex:form>
 
    <apex:outputPanel title="" id="error">
     <apex:pageMessages ></apex:pageMessages>
     </apex:outputPanel>
 
    <apex:pageBlock title="Accounts" id="acct">
    <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 
 <apex:pageBlock title="Contacts" id="cont">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  
 <apex:pageBlock title="Opportunities" id="oppt">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
   
</apex:page>

Controller:
 
Public with sharing class SOSLController{
 Public List<Opportunity> optyList {get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
  
 Public String searchStr{get;set;}
   Public SOSLController(){
   }
  
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   conList = New List<contact>();
   accList = New List<account>();
   if(searchStr.length() > 1){
   String searchStr1 = '*'+searchStr+'*';
   String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName)';
   List<List <sObject>> searchList = search.query(searchQuery);
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   if(accList.size() == 0 && conList.size() == 0 && optyList.size() == 0){
       apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..'));
       return;
   }
   }
   else{
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..'));
   return;
   }
  }
}

 
This was selected as the best answer
BenBurbridge(JP)BenBurbridge(JP)
Thanks both .... 

Wizno @ Configero: I have added the method you suggested and it is working sporadically and not returning all searches.

Matthews: Can you offer further explanation on your suggestion.

Ben
Wizno @ ConfigeroWizno @ Configero
Here's one thing I forgot to mention. SOSL only does not search all fields:
 
The search() call searches most objects (including custom objects) and text fields to which you have access. It does not search the following objects and fields:
 
Any elements such as picklists that are defined as not searchable (searchable is false). To determine whether a given object is searchable, your application can invoke the describeSObjects() call on the object and inspect the searchable property in the DescribeSObjectResult.
Number, date, or checkbox fields. To search for such information, use the query() call instead.
Textarea fields, unless you use the ALL FIELDS search group.
Attachment records associated with certain objects, such as Account, Contact, or Opportunity.

 
Wizno @ ConfigeroWizno @ Configero
Here's some additional documentation on SOSL: http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_sosl.htm
BenBurbridge(JP)BenBurbridge(JP)
Great help Wizno, like I said the route you supplied worked well but I couldnt get some searches to work.  Strange really, I feel it is probably something to do with the changes I have made but they are minor and are to do with specific objects we use here.
vnbhargavivnbhargavi
Hi All,

Can any one hekp how to use multiple search in SOSL queries like.
String name{get;set;}
string email {get;set;}
List<List<SObject>> searchList = [find :companyName OR :email IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
        
I am getting error with the above query...