Adding Search functionality for related lists

A typical salesforce instance usually starts out by leveraging as much standard functionality as possible. One such standard functionality is related lists – usually the first release of most salesforce implementations use related lists for contacts, opportunities, leads etc. As the amount of data increases, and the system and it’s users mature, someone might throw in a curveball and ask for search functionality on related lists- for e.g., someone might say “I have 500+ contacts on a company, and I don’t want to page through the entire list one by one”.
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 :-
image
And here’s the Visualforce page for it :-
Code Snippet
  1. <apex:page standardController="Account" extensions="Contact_Searcher">
  2.   < apex:form >
  3.     <apex:pageBlock title="Contact Search">
  4.       <!-- I got the <h2> tag with the CSS name by viewing the source of my salesforce page
  5.         Another neat thing to note is the use of &nbsp; to add a white space -->
  6.       <h2 class="maintitle">Enter Search String&nbsp;&nbsp;</ h2 >
  7.       <apex:inputText id="searchBox" value="{!searchValue}" />
  8.       <apex:commandButton id="submit" value="Search" action="{!searchContacts}" />
  9.     </ apex:pageBlock >
  10.     <apex:pageBlock title="Search Results">
  11.       <apex:pageBlockTable value="{!searchResults}" var="c">
  12.         <apex:column value="{!c.Name}" />
  13.       </ apex:pageBlockTable >
  14.     </ apex:pageBlock >
  15.   </ apex:form >
  16. </ apex:page >
Here’s the Apex class :-
Code Snippet
  1. // create the custom controller extension class
  2. public class Contact_Searcher {
  3.     // Since we are creating an extension to the account standard controller,
  4.     // create an account object to hold the current account
  5.     Account a;
  6.     public Contact_Searcher(ApexPages.StandardController controller)
  7.     {
  8.         // Get the current account, and store it in the account object
  9.         a = (Account) controller.getRecord();
  10.     }
  11.    
  12.     public string searchValue
  13.     {
  14.         get
  15.         {
  16.             if(searchValue == null)
  17.                 searchValue = '';
  18.             return searchValue;
  19.         }
  20.         set;
  21.     }
  22.    
  23.     public List<Contact> searchResults
  24.     {
  25.         get
  26.         {
  27.             if(searchResults == null)
  28.                 searchResults = new List<Contact>();
  29.             return searchResults;
  30.         }
  31.         set;
  32.     }
  33.        
  34.    
  35.     // No need to return the result set. We will just assign to the class variable
  36.     public void searchContacts()
  37.     {
  38.         if(searchValue == '')
  39.             return;
  40.         // Output the search value for debugging
  41.         System.Debug('Initializing search, keyword is : ' + searchValue);       
  42.         String finalSearchValue = '%' + searchValue + '%';
  43.         List<Contact> contacts = new List<contact>();
  44.         // Careful- check out what the SOQL query is doing.
  45.         // It gets a list of contacts under that account with matching names.
  46.         contacts = [select Id, Name from Contact where Account.Id = :ApexPages.CurrentPage().getParameters().get('Id')
  47.                     and Name like :finalSearchValue];
  48.         searchResults = contacts;
  49.     }
  50. }

No comments:

Post a Comment