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
roysmith601.3493067652527424E1roysmith601.3493067652527424E1 

how to add search button in visual force page

Hi,

I am working on visualforce page on lead object and this visualforce page will be used when user click on the "new(already exist) " button then this new VF page popup and to make sure that user not creating duplicate lead so i add field: first name,last name and email in that page.when user enter the information then userclick on "search " button and if there no lead matches with user input then he/she be able to create the new lead. i do know that how to "search button " in the vf page.

here is the VF code:

<apex:page standardController="Lead" recordSetVar="leads" sidebar="true" showHeader="true" standardStylesheets="true" tabStyle="lead" >
<style>
.activeTab {background-color: #236FBD; color:white;background-image:none}
.inactiveTab { background-color: lightgrey; color:black;background-image:none}
</style>

  <apex:form >
    <apex:pageBlock >
          <apex:pageMessages />
            <apex:pageBlockButtons >
              <apex:commandButton value="Edit" action="{!save}" id="editButton" />
              <apex:commandButton value="Save" action="{!save}" id="saveButton" />
              <apex:commandButton action="{! cancel}" value="Cancel!" immediate="true"/>
            </apex:pageBlockButtons>
       <apex:pageBlockSection title="Search for Duplicates" columns="1" >
           <apex:inputField value="{!lead.Firstname}"/>
           <apex:inputField value="{!lead.lastname}"/>
           <apex:inputField value="{!lead.company}"/>
           <apex:inputField value="{!lead.email}"/>
           <apex:inputField value="{!lead.phone}"/>
       </apex:pageBlockSection>
       
    </apex:pageBlock>
   </apex:form>
</apex:page>

 

 

please help me

thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Saurabh DhobleSaurabh Dhoble

You cannot get the entire thing only with a standard controller, there are multiple steps in this process.

 

Step 1 - Create a new class that will serve as an extension to Lead. The class should have two things - (A) a "searchText" property, that we use for the search text, and (B) a "search" method, that does the actual search. Here's what I could get :-

 

public class SearchController
{
private apexpages.standardController controller {get; set; }
private Lead l;
public List<Lead> searchResults {get; set; }
public string searchText
{
get
{
if (searchText==null) searchText = '';
return searchText;
}
set;
}

public SearchController(ApexPages.StandardController controller)
{
this.controller = controller;
this.l = (Lead) controller.getRecord();
}

public PageReference search()
{
if(SearchResults == null)
{
SearchResults = new List<Lead>();
}
else
{
SearchResults.Clear();
}

String qry = 'Select Id, Name from Lead where name like \'%'+searchText+'%\') Order By Name';
SearchResults = Database.query(qry);
return null;
}
}

 

 

Step 2:- Add he controller to your page as an extension

<apex:page standardController="Lead" extensions="SearchController">

 Step 3:- Hook up the searchText and the output grid

    <apex:form id="searchForm">
        <apex:PageBlock mode="edit">        
        <apex:pageblockSection id="searchBlockSection">
            <apex:pageBlockSectionItem id="searchBlockSectionItem">
                <apex:outputLabel >Keyword</apex:outputLabel>
                <apex:panelGroup >
                    <apex:inputtext id="searchTextBox" value="{!searchText}">
                        
                    </apex:inputtext>
                    <apex:commandButton Id="btnSearch" action="{!search}" rerender="renderBlock" status="status" title="Search" value="Search">                    </apex:commandButton>
                </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageblockSection>
        <apex:actionStatus id="status" startText="Searching... please wait..."/>        
        <apex:pageBlocksection id="renderBlock" >
            <apex:pageblocktable value="{!SearchResults}" var="o" rendered="{!NOT(ISNULL(SearchResults))}">
                <apex:outputLink value="/{!o.Id}">{!o.Name}</apex:outputLink>
                <apex:column value="{!o.Id}"/>
            </apex:pageblocktable>        </apex:pageBlocksection>

 

This should give you a search box. I haven't tested the code, so you might have to fix some syntax errors etc., but this is the general direction you can take to solve this problem.

 

 

 

All Answers

Saurabh DhobleSaurabh Dhoble

You cannot get the entire thing only with a standard controller, there are multiple steps in this process.

 

Step 1 - Create a new class that will serve as an extension to Lead. The class should have two things - (A) a "searchText" property, that we use for the search text, and (B) a "search" method, that does the actual search. Here's what I could get :-

 

public class SearchController
{
private apexpages.standardController controller {get; set; }
private Lead l;
public List<Lead> searchResults {get; set; }
public string searchText
{
get
{
if (searchText==null) searchText = '';
return searchText;
}
set;
}

public SearchController(ApexPages.StandardController controller)
{
this.controller = controller;
this.l = (Lead) controller.getRecord();
}

public PageReference search()
{
if(SearchResults == null)
{
SearchResults = new List<Lead>();
}
else
{
SearchResults.Clear();
}

String qry = 'Select Id, Name from Lead where name like \'%'+searchText+'%\') Order By Name';
SearchResults = Database.query(qry);
return null;
}
}

 

 

Step 2:- Add he controller to your page as an extension

<apex:page standardController="Lead" extensions="SearchController">

 Step 3:- Hook up the searchText and the output grid

    <apex:form id="searchForm">
        <apex:PageBlock mode="edit">        
        <apex:pageblockSection id="searchBlockSection">
            <apex:pageBlockSectionItem id="searchBlockSectionItem">
                <apex:outputLabel >Keyword</apex:outputLabel>
                <apex:panelGroup >
                    <apex:inputtext id="searchTextBox" value="{!searchText}">
                        
                    </apex:inputtext>
                    <apex:commandButton Id="btnSearch" action="{!search}" rerender="renderBlock" status="status" title="Search" value="Search">                    </apex:commandButton>
                </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageblockSection>
        <apex:actionStatus id="status" startText="Searching... please wait..."/>        
        <apex:pageBlocksection id="renderBlock" >
            <apex:pageblocktable value="{!SearchResults}" var="o" rendered="{!NOT(ISNULL(SearchResults))}">
                <apex:outputLink value="/{!o.Id}">{!o.Name}</apex:outputLink>
                <apex:column value="{!o.Id}"/>
            </apex:pageblocktable>        </apex:pageBlocksection>

 

This should give you a search box. I haven't tested the code, so you might have to fix some syntax errors etc., but this is the general direction you can take to solve this problem.

 

 

 

This was selected as the best answer
roysmith601.3493067652527424E1roysmith601.3493067652527424E1

thanks for you help.Now i want to add "Next" button in the vf page so that it take user to next page incase there is no lead exists already to creat new lead.

thanks

Saurabh DhobleSaurabh Dhoble

Try creating Previous & Next methods in the SearchController class.

For e.g.,

 

public void Previous()
{
     l.Previous();
}

 Then in your code, create two links that call this method, this should give you the paging ability. Checkout http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/ for details.

 

<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
Abdul Mujeeb ShaikAbdul Mujeeb Shaik

In the text-box user enters the name: the code should fetch the details of that name and displayied in a Table.
-----

 


vF code:
--------
<apex:page sidebar="false" controller="searchcontrol1" >
<apex:form >

<apex:outputLabel style="font-weight:bold;" value="Search By Name:" ></apex:outputLabel>
<apex:inputText value="{!textData}"/>
<apex:commandButton value="Search" action="{!result}"/>
<br/> <br/>
<apex:pageBlock >

<apex:pageBlockTable value="{!bt}" var="nxt">
<apex:column value="{!nxt.name}"/>
</apex:pageBlockTable>
</apex:pageBlock>

</apex:form>
</apex:page>

}
Apex Controller:
--------------------
public with sharing class searchcontrol1 {
public LIST<Bottle__c> bt { get; set; }

 

public PageReference result() {

List<Bottle__c> bt =[select Name from Bottle__c where Name=:textData];


return null;
}

public String textData { get; set; }
}

-------------------------------------

No Error and No Out put .please somebody help to complete this task