This might sound easy, but it is definitely not. Problem is, you cannot add a custom search textbox to your standard layout. Even if you just want to plug-in the search textbox into the page layout related list, you cannot do so. Instead, you need to create a new visual force page, add all the functioality. Here’s what a basic search field I created looks like :-
And here’s the Visualforce page for it :-
Code Snippet
- <apex:page standardController="Account" extensions="Contact_Searcher">
- < apex:form >
- <apex:pageBlock title="Contact Search">
- <!-- I got the <h2> tag with the CSS name by viewing the source of my salesforce page
- Another neat thing to note is the use of to add a white space -->
- <h2 class="maintitle">Enter Search String </ h2 >
- <apex:inputText id="searchBox" value="{!searchValue}" />
- <apex:commandButton id="submit" value="Search" action="{!searchContacts}" />
- </ apex:pageBlock >
- <apex:pageBlock title="Search Results">
- <apex:pageBlockTable value="{!searchResults}" var="c">
- <apex:column value="{!c.Name}" />
- </ apex:pageBlockTable >
- </ apex:pageBlock >
- </ apex:form >
- </ apex:page >
Code Snippet
- // create the custom controller extension class
- public class Contact_Searcher {
- // Since we are creating an extension to the account standard controller,
- // create an account object to hold the current account
- Account a;
- public Contact_Searcher(ApexPages.StandardController controller)
- {
- // Get the current account, and store it in the account object
- a = (Account) controller.getRecord();
- }
- public string searchValue
- {
- get
- {
- if(searchValue == null)
- searchValue = '';
- return searchValue;
- }
- set;
- }
- public List<Contact> searchResults
- {
- get
- {
- if(searchResults == null)
- searchResults = new List<Contact>();
- return searchResults;
- }
- set;
- }
- // No need to return the result set. We will just assign to the class variable
- public void searchContacts()
- {
- if(searchValue == '')
- return;
- // Output the search value for debugging
- System.Debug('Initializing search, keyword is : ' + searchValue);
- String finalSearchValue = '%' + searchValue + '%';
- List<Contact> contacts = new List<contact>();
- // Careful- check out what the SOQL query is doing.
- // It gets a list of contacts under that account with matching names.
- contacts = [select Id, Name from Contact where Account.Id = :ApexPages.CurrentPage().getParameters().get('Id')
- and Name like :finalSearchValue];
- searchResults = contacts;
- }
- }
No comments:
Post a Comment