+ Start a Discussion
Tanay MathurTanay Mathur 

Visual force searchbar implementation

Hi All,

As i`ve been new to salesforce , i`ve been learning on my own 
i encountered a problem trying to make own serach bar using visualforce . the code was accepted error free , but it`s not working 
The idea is to search a name and you`ll get his address as a return

Visualforce Page:
<apex:page controller="Ctrl_ContactSearch">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Search" action="{!searchContacts}" reRender="contact-table" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection id="contact-table" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name" />
                    <apex:inputText value="{!name}" />
                </apex:pageBlockSectionItem>

             

                <apex:pageBlockTable value="{!contacts}" var="c">
                    <apex:column >
                        <apex:facet name="header">Name</apex:facet>
                        {!c.Name}
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">Mailing State</apex:facet>
                        {!c.MailingState}
                    </apex:column>
                    
                     <apex:column >
                        <apex:facet name="header">Mailing Street</apex:facet>
                        {!c.MailingStreet  }
                    </apex:column>
                    
                    <apex:column >
                        <apex:facet name="header">Mailing City</apex:facet>
                        {!c.MailingCity}
                    </apex:column>
                    
                    <apex:column >
                        <apex:facet name="header">Mailing Country</apex:facet>
                        {!c.MailingCountry}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Contoller :

public with sharing class Ctrl_ContactSearch
{
    public List<Contact> contacts { get; set; }
    public String name { get; set; }
    public String mailingState { get; set; }
     public String mailingStreet { get; set; }
      public String mailingCity { get; set; }
       public String mailingCountry { get; set; }

    public Ctrl_ContactSearch()
    {
        contacts = new List<Contact>();
    }

    public PageReference searchContacts()
    {
        contacts = [select Id
                          ,Name
                          ,MailingState ,
                          MailingStreet,
                          MailingCity,
                          MailingCountry
                     from Contact 
                    where Name = :name and
                    MailingState = :mailingState and
                   MailingStreet = :mailingStreet and
                   MailingCity = :mailingCity and
                   MailingCountry = :mailingCountry];
        return null;
    }
}
Prakash NawalePrakash Nawale
Hi Tanay Mathur,

can you please follow below code and try to implement.


public with sharing class accsearchcontroller { 
public list <account> acc {get;set;} 
public string searchstring {get;set;} 
public accsearchcontroller( ) { 

public void search(){ 
string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
acc= Database.query(searchquery); 

public void clear(){ 
acc.clear(); 

}
Take a look at the code for the Visualforce page:
<apex:page Controller="accsearchcontroller" > 
<apex:form > 
<apex:inputText value="{!searchstring}" label="Input"/> 
<apex:commandButton value="Search records" action="{!search}"/> 
<apex:commandButton value="Clear records" action="{!clear}"/> 
<apex:pageBlock title="Search Result"> 
<apex:pageblockTable value="{!acc}" var="a"> 
<apex:column value="{!a.name}"/> 
<apex:column value="{!a.id}"/> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:form> 
</apex:page>