Monday, December 30, 2013

Search Page in Visualforce-Simplified

Here is a simple example how to build a simple Visualforce search page. When I started learning Visualforce I thought building a search page is too complex, but it's not true. We can modify the wrapper class example which I posted earlier to achieve this. Nothing much to dig deeper into the code. The code is very simple just passing the get set parameters from a text field to the controller query using the 'LIKE'clause. Here is the controller for it to give you a clear picture.

 public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20';  
     acc= Database.query(searchquery);  
   }  
   public void clear(){  
   acc.clear();  
   }  
 }  

    let's see what I have done here. I have created a string variable searchstring and used the variable in the LIKE query. This search string gets the input from the text box in the visualforce page where user will enter the data and again passes the query string variable inside the database.query() method. If you look at the visual force page code

 <apex:page standardController="account" extensions="accsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!a.id}"/>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>  



Here is the button action method "Search" which returns the list of records. It's simple isn't it?

8 comments:

Raphael Krausz said...

Thanks for posting, good article.

A few suggestions.

There is a small error:


Should be:



Have you thought about escaping the input?

E.g. String.escapeSingleQuotes(searchQuery)

This would help prevent anyone manipulating the database in a way you wouldn't want via your query.

You may also wish to add an pageMessages section for errors:



You could then throw an error for an empty search or if the database throws an error.

Thanks again for posting. This is a great "bare bones" example!

Nirmal said...

It's a good idea to add error messages but i wanted to make the code as simple as I can and the Issue about SOQL injection I will come up with a new blog soon modifying the same code so the readers can understand it well :)

Anonymous said...

Hi, I am new to programming/development. I tried making a few changes to your code with no luck because I want to make a Product search instead of Account. How would I change the code to do so? Any help would be appreciated.

Nirmal said...

You just need to replace all the the account object related components in the code to your custom object.

Raj said...

Nice one Nirmal!!
Exactly great starter article I was looking for

Raj said...

Than you..

Unknown said...

I calculated to compose you one slight note to comfort say thanks over again on the breathtaking tactics you’ve provided in this case. It is so necessary to sit back every once and a while and just be thankful.

Boostedcrm.com was given the awareness of being the leading Zoho Solution Provider, with a description of their modernization and innovation in finding the creative solution.

Anonymous said...

This is an old post however very useful. Do you mind posting the test class? I'm having hard time with it.
Thanks a million