4.05.2010

Simple Search with SOQL

I've added a simple search feature into my Force.com-powered guestbook app (see previous post). This search feature will take keyword(s) and search the comments with Force.com SOQL.

One very important catch that I discovered, we cannot use SOQL to filter long text area custom field type. I have to change the custom field type for my Comment__c from ‘long text area’ into ‘text area’ (255 char max). I still don’t know what would be the best way to filter long text area field. I might try searching using SOSL.

To try the search, please go to URL below and navigate to "Search Comments".
http://cewl-developer-edition.na7.force.com/listEntries

I enhanced the Apex Controller with several attributes and methods
// Apex Controller
public with sharing class gbEntryController {
  /** Class properties */
  ApexPages.StandardController controller;
  String searchKeyword;  // capture the search keyword
  gbEntry__c[] gbEntries;  // search result records holder
  Integer noSearchResults = 0;  // get the no. of search result
  /** Class Constructor */
  public gbEntryController(ApexPages.StandardController controller) {
    this.controller = controller;
 } 
  /** Getter & Setter methods */
  public void setSearchKeyword(String searchKeyword) {
    this.searchKeyword = searchKeyword;
  }
  public String getSearchKeyword() {
    return this.searchKeyword;
  }
  public gbEntry__c[] getGbEntries() {
    return this.gbEntries;
  }
  public Integer getNoSearchResults() {
    return this.noSearchResults;
  }
  /** Save method – save the comment and redirect user to list of comments */
  public PageReference save() {
    this.controller.save();
    PageReference pageRef = new PageReference('/apex/listEntries');
    return pageRef;
  }
  /** SearchEntries method – search comments based on the submitted keyword */
  public void searchEntries() {
    if (searchKeyword != null && !searchKeyword.trim().equals('')) {
      String likeStr = '%' + searchKeyword + '%';  
      this.gbEntries = [SELECT id, Name__c, Comment__c, CreatedDate 
                       FROM gbEntry__c WHERE Comment__c like :likeStr 
                       ORDER BY CreatedDate ASC  LIMIT 100];
      this.noSearchResults = (
      this.gbEntries == null) ? 0 : this.gbEntries.size();
    }   
  }
}
This is the new visualforce page. The "action" attribute is used to call the gbEntryController's searchEntries method. The result will be displayed with partial page refresh with AJAX provided by the Force.com Visualforce by using the "rerender" attribute & supplying an ID of an output panel.
// Visualforce code
<apex:page standardcontroller="gbEntry__c" 
  extensions="gbEntryController" showHeader="false">
  <div style="width:640px;margin:20px;padding:20px">
  <h1>Force.com Powered Guest Book</h1>
  <p><a href="listEntries">View Comments</a> | <a href="submitComment">Leave Comment</a> | Search Comments</p>
  <hr style="border:1px solid silver"/> 
  <apex:form >
    <table cellpadding="2" cellspacing="3">
      <tr>
        <td valign="top" colspan="2"><apex:pageMessages /></td>
      </tr><tr>   
        <td valign="top"><apex:inputText style="width:200px;" value="{!searchKeyword}"/></td>
 <td><apex:commandButton value="Search" action="{!SearchEntries}" rerender="searchResult"/></td>
      </tr> 
    </table>
  </apex:form>
  <hr style="border:1px solid silver"/>
  <apex:outputPanel id="searchResult"> 
    <apex:repeat value="{!GbEntries}" var="gbEntry" rendered="{!NOT(ISNULL(GbEntries))}">
      <div style="padding:10px;margin:10px;border:1px dotted silver; background-color:#F9B7FF">
        <span style="font-size: smaller">
   <apex:outputField style="font-family: Monospace;" value="{!gbEntry.Comment__c}"/>
   <br/><br/>
   Posted by&nbsp;<strong><apex:outputField value="{!gbEntry.Name__c}"/></strong> 
          at &nbsp;<apex:outputField value="{!gbEntry.createdDate}"/>
 </span>
      </div> 
    </apex:repeat>
    <strong>{!noSearchResults}</strong> records found.</apex:outputPanel> 
  </div> 
</apex:page>

2 comments:

patrick said...

Its very nice,thanks for sharing... And one second, you need for your dreamily websites download Web design and developments

kaviya said...

Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun. thank u



Bay Area Web Developer