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
Waseem AkramWaseem Akram 

search list on vf page

Hello Guys,
                   I need a search list button on vf page.  when we type for any keyword to search it should display all the items related to that word. How to achieve this?
Best Answer chosen by Waseem Akram
jyothsna reddy 5jyothsna reddy 5
Hi Waseem Akram,

Searching the text string across the object and across the field will be done by using SOSL. This is Salesforce Object Search Language. It has capability of searching a particular string across the multiple object.
SOSL statements evaluate to a list of sObjects, where each list contains the search results for a particular sObject type. The result lists are always returned in the same order as they were specified in the SOSL query.

  I can give you code sample please go through it and try to implement your requirement.

 
Visual force 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;
   }

}
Controller
 
<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>

If the answer helped you, mark it as solved.

Thank you

All Answers

Ramon PereiraRamon Pereira
Hello,
Salesforce has a very powerful feature called SOQL.
You can get more information here (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl.htm) and example here (http://www.sfdcpoint.com/salesforce/sosl-example-in-salesforce/).

You can put the button and return to users which objects were found with the searched word.

If the answer helped you, mark it as resolved.
thank you
jyothsna reddy 5jyothsna reddy 5
Hi Waseem Akram,

Searching the text string across the object and across the field will be done by using SOSL. This is Salesforce Object Search Language. It has capability of searching a particular string across the multiple object.
SOSL statements evaluate to a list of sObjects, where each list contains the search results for a particular sObject type. The result lists are always returned in the same order as they were specified in the SOSL query.

  I can give you code sample please go through it and try to implement your requirement.

 
Visual force 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;
   }

}
Controller
 
<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>

If the answer helped you, mark it as solved.

Thank you
This was selected as the best answer