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
Chris MarzellaChris Marzella 

Create a search on a visualforce page

What is the simplist way to take user input and do a search on specific fields based on their input?

Ex: they have fields like name and age available//they input John and 23//search returns all John's with an age of 23.

It doesn't need to be anything fancy. The simpler the better.
ClintLeeClintLee
Hi Chris,

The code below will show you how to create a Visualforce page that takes two inputs - Name and MailingState.  The page has a button named Search that, when clicked, will query all Contacts and return those that have a matching Name and MailingState.  

The button is connected to a searchContacts() method that handles the query.  The results are displayed in a pageBlockTable.

This is really simple but should point you in the right direction as to how to accomplish this.

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

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

    public PageReference searchContacts()
    {
        contacts = [select Id
                          ,Name
                          ,MailingState 
                     from Contact 
                    where Name = :name
                    and MailingState = :mailingState];
        return null;
    }
}
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:pageBlockSectionItem>
                    <apex:outputLabel value="Mailing State" />
                    <apex:inputText value="{!mailingState}" />
	            </apex:pageBlockSectionItem>

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

                    <apex:column>
                        <apex:facet name="header">
                            {!c.MailingState}
                        </apex:facet>
                    </apex:column>
                </apex:pageBlockTable>
	        </apex:pageBlockSection>
	    </apex:pageBlock>
    </apex:form>
</apex:page>

Hope that helps,

Clint
 
Balaji BondarBalaji Bondar
Hi Chris,

Please find simple search code at below location:
http://gtr.net/how-to-build-a-simple-search-page-using-visualforce/
ClintLeeClintLee
I made an error on the Visualforce page above. Here's a re-do.
 
<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:pageBlockSectionItem>
                    <apex:outputLabel value="Mailing State" />
                    <apex:inputText value="{!mailingState}" />
	            </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:pageBlockTable>
	        </apex:pageBlockSection>
	    </apex:pageBlock>
    </apex:form>
</apex:page>

 
GohadGohad

I am using this code for public visualforce page, I am getting empty rows like this

User-added image