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
Uttirna DasUttirna Das 

Search functionality in vf page

I want to give search option for names of custom object menu list in vf page . Struggling in coding.

Please help me
Best Answer chosen by Uttirna Das
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Uttirna Das,

Please refer the below code for Search Functionality by Name.

VF Page:
<apex:page controller="searchname1">
  <apex:form >
   <apex:pageblock id="one" >
    <apex:pageblockSection >
    <apex:pageblocksectionitem >
       <apex:outputlabel >Name</apex:outputlabel>
         <apex:inputtext value="{!name}"/>
         </apex:pageblocksectionitem>
         <apex:commandButton value="go" action="{!search}"/>
          </apex:pageblockSection>
           </apex:pageBlock> 
           <apex:pageBlock rendered="{!searched}" >
            <apex:pageblocktable value="{!mycar}" var="c">
            <apex:column value="{!c.name}"/>
             <apex:column value="{!c.Price__c}"/>
            </apex:pageblocktable>
            <apex:pageblockbuttons >
             <apex:commandButton value="edit" action="{!edit}"/>
            </apex:pageblockbuttons>
               </apex:pageblock>
  </apex:form>
</apex:page>

Apex Code:
public with sharing class searchname1 {

    public PageReference edit() {
       
        return null;
    }


   // public String mycar { get; set; }
   public String name { get; set; }
    public list<car__c> mycar { get; set; }
    public boolean searched{get;set;}
    
    //default constructor
    public searchname1(){
    searched=false;
    string namestr=apexpages.currentpage().getparameters().get('name');
    if(null!=namestr){
    name=namestr;
    }
    }
    public PageReference search() {
    searched=true;
     string searchstr1=('%'+name+'%');
    mycar=[select id,Name,price__c from car__c where name like :searchstr1 limit 10 ];
        return null;
    }


    
}

I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar

All Answers

AvaneeshAvaneesh
Hi Uttirna 
read this jeff douglas blog and he gave a complete solution of Search functionality.

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

Thank you 
Avaneesh Singh
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Uttirna Das,

Please refer the below code for Search Functionality by Name.

VF Page:
<apex:page controller="searchname1">
  <apex:form >
   <apex:pageblock id="one" >
    <apex:pageblockSection >
    <apex:pageblocksectionitem >
       <apex:outputlabel >Name</apex:outputlabel>
         <apex:inputtext value="{!name}"/>
         </apex:pageblocksectionitem>
         <apex:commandButton value="go" action="{!search}"/>
          </apex:pageblockSection>
           </apex:pageBlock> 
           <apex:pageBlock rendered="{!searched}" >
            <apex:pageblocktable value="{!mycar}" var="c">
            <apex:column value="{!c.name}"/>
             <apex:column value="{!c.Price__c}"/>
            </apex:pageblocktable>
            <apex:pageblockbuttons >
             <apex:commandButton value="edit" action="{!edit}"/>
            </apex:pageblockbuttons>
               </apex:pageblock>
  </apex:form>
</apex:page>

Apex Code:
public with sharing class searchname1 {

    public PageReference edit() {
       
        return null;
    }


   // public String mycar { get; set; }
   public String name { get; set; }
    public list<car__c> mycar { get; set; }
    public boolean searched{get;set;}
    
    //default constructor
    public searchname1(){
    searched=false;
    string namestr=apexpages.currentpage().getparameters().get('name');
    if(null!=namestr){
    name=namestr;
    }
    }
    public PageReference search() {
    searched=true;
     string searchstr1=('%'+name+'%');
    mycar=[select id,Name,price__c from car__c where name like :searchstr1 limit 10 ];
        return null;
    }


    
}

I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar
This was selected as the best answer