You need to sign in to do that
Don't have an account?
R 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.
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.
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
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:
Controller of Visalforce Page
Apex Code:
Thanks,
David(1040)
Hi R R M,
Your requrment will look like:
Thanks,
David(1040)
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);
}
}
}
}