+ Start a Discussion
R R MR R M 

How to Search Accounts Based on Using Selected Field on Visualforce page

Hi Folks, 

Want to search account on Visualforce page and based on Selected Option. 
Ex : Name, Phone, Email.
Above there parameters need to add like in a picklist on Visualforce page and there should be an option to enter some characters and the search Button. 
Then based on selected option and entered value data should query and display. 
Please help. 

Thanks in Advance.
Best Answer chosen by R R M
Anas Yar KhanAnas Yar Khan
Hi R R M
Hope this will help you.
Use email instead of webiste.

Visualforce Page:

<apex:page controller="SearchPageController" sidebar="false" showHeader="false">
    <apex:form >
        <apex:messages />
        <apex:pageBlock mode="edit">
            <apex:pageBlockSection columns="2">
                <apex:selectList value="{!selectField}" multiselect="false" size="1">
                    <apex:selectOptions value="{!listSelectOption}" />
                </apex:selectList>
                <apex:inputText value="{!inputText}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock> 
        <apex:pageBlock title="Related Account" mode="edit" rendered="{!showList}">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!listAcc}" var="acc">
                    <apex:column headerValue="Name" >
                        <apex:outputField value="{!acc.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Phone" >
                        <apex:outputField value="{!acc.Phone}"/>
                    </apex:column>
                    <apex:column headerValue="Website" >
                        <apex:outputField value="{!acc.Website}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class SearchPageController {
    public String selectField {get;set;}
    public String inputText {get;set;}
    public List<selectOption> listSelectOption {get;set;}
    public List<Account> listAcc {get;set;}
    public Boolean showList {get;set;} 
    public SearchPageController() {
        showList = false;
        inputText = '';
        listSelectOption = new List<selectOption>();
        listSelectOption.add(new selectOption('','---None---'));
        listSelectOption.add(new selectOption('Name','Name'));
        listSelectOption.add(new selectOption('Phone','Phone'));
        listSelectOption.add(new selectOption('Website','Website'));
        listAcc = new List<Account>();
    }
    public void search() {
        
        if(selectField == NULL)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select Field'));
        
        if(inputText == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value in Search Box'));    
        
        if(selectField != NULL && inputText != '') {
            showList = true;
            if(selectField == 'Name') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Name =:inputText';
                listAcc = Database.query(query);
            }
            if(selectField == 'Phone') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Phone =:inputText';
                listAcc = Database.query(query);
            } 
            if(selectField == 'Website') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Website =:inputText';
                listAcc = Database.query(query);
            }
        }
        
    }
}

All Answers

David HalesDavid Hales

Hi R R M,

As I found in your requirement, you ask for Email field too to retrieve account information which 
is not a Standard field of Account Object that may be a custom field on Account Object.

Ex: Name, Phone, Email(Instant-on Email I use Website Field In Demo Code).

Visualforce Page:

<apex:page controller="clsAccountSearch" sidebar="false" showHeader="false">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection title="Account Search">
              <apex:selectList title="Select Field" label="Select Field" value="{!selectedField}" multiselect="false" size="1">
                <apex:selectOption itemValue="Name" itemLabel="Name"/>
                <apex:selectOption itemValue="Website" itemLabel="Website"/>
                <apex:selectOption itemValue="Phone" itemLabel="Phone"/>
            </apex:selectList>
            <apex:inputText label="Search" value="{!searchValue}"/>
            <apex:commandButton value="Search" action="{!searchAccount}"/>
          </apex:pageBlockSection>
          <apex:pageBlockTable value="{!SelectedAcount}" var="item" >
              <apex:column value="{!item.name}"/>
              <apex:column value="{!item.Phone}"/>
          </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Controller of Visalforce Page 
Apex Code:

public class clsAccountSearch {
    public String selectedField{get;set;}
    public String searchValue{get;set;}
    public List<Account>SelectedAcount{get;set;}
    public ClsAccountSearch()
    {   
        selectedField=''; 
        searchValue='';
    }
    public void searchAccount()
    {
        String Query='select Name,Phone from Account where '+selectedField+' =: searchValue';
        System.debug('@Query'+Query);
        SelectedAcount=Database.query(Query);
    }
}

Thanks,

David(1040)

David HalesDavid Hales

Hi R R M,

Your requrment will look like:

User-added image

Thanks,

David(1040)

 

Anas Yar KhanAnas Yar Khan
Hi R R M
Hope this will help you.
Use email instead of webiste.

Visualforce Page:

<apex:page controller="SearchPageController" sidebar="false" showHeader="false">
    <apex:form >
        <apex:messages />
        <apex:pageBlock mode="edit">
            <apex:pageBlockSection columns="2">
                <apex:selectList value="{!selectField}" multiselect="false" size="1">
                    <apex:selectOptions value="{!listSelectOption}" />
                </apex:selectList>
                <apex:inputText value="{!inputText}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock> 
        <apex:pageBlock title="Related Account" mode="edit" rendered="{!showList}">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!listAcc}" var="acc">
                    <apex:column headerValue="Name" >
                        <apex:outputField value="{!acc.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Phone" >
                        <apex:outputField value="{!acc.Phone}"/>
                    </apex:column>
                    <apex:column headerValue="Website" >
                        <apex:outputField value="{!acc.Website}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class SearchPageController {
    public String selectField {get;set;}
    public String inputText {get;set;}
    public List<selectOption> listSelectOption {get;set;}
    public List<Account> listAcc {get;set;}
    public Boolean showList {get;set;} 
    public SearchPageController() {
        showList = false;
        inputText = '';
        listSelectOption = new List<selectOption>();
        listSelectOption.add(new selectOption('','---None---'));
        listSelectOption.add(new selectOption('Name','Name'));
        listSelectOption.add(new selectOption('Phone','Phone'));
        listSelectOption.add(new selectOption('Website','Website'));
        listAcc = new List<Account>();
    }
    public void search() {
        
        if(selectField == NULL)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select Field'));
        
        if(inputText == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value in Search Box'));    
        
        if(selectField != NULL && inputText != '') {
            showList = true;
            if(selectField == 'Name') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Name =:inputText';
                listAcc = Database.query(query);
            }
            if(selectField == 'Phone') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Phone =:inputText';
                listAcc = Database.query(query);
            } 
            if(selectField == 'Website') {
                String query = 'SELECT Id,Name,Phone,Website FROM Account WHERE Website =:inputText';
                listAcc = Database.query(query);
            }
        }
        
    }
}
This was selected as the best answer