Most of the times client ask for implementation of standard functionality through customization as they find hard to have an ADMIN for them, # of logins, mobile app development, etc.

I recently came across such situation with my client. We do have search on standard Knowledge Tab of Salesforce. But we are asked to implement same functionality on Visualforce page.

So, here we go

1. I have designed a visualforce page with a search bar and a plageblock to display the search results. Using Apex Action Support on event KeyUp, I am passing the user entered SearchString to controller and getting the results with SOQL query.

2. On Controller, I have declared the String variable which holds the Search string and using LIKE keyword of SOQL query, I am getting the results into a List.

Visualforce Page:


 <apex:page controller="articleSearchController">  
  <apex:form >  
    <apex:pageBlock >  
     Article Name:<apex:inputText value="{!searchstring}" >  
      <apex:actionSupport action="{!getSearchResults}" event="onkeyup" reRender="outptText" />  
     </apex:inputtext>  
    </apex:pageBlock>   
   <apex:pageblock title="Search Results">  
    <apex:pageblocktable value="{!articleList}" var="acc" id="outptText">  
      <apex:column >  
        <apex:outputLink value="https://na35.salesforce.com/articles/{!acc.ArticleType}/{!acc.UrlName}">{!acc.Title}</apex:outputLink>  
      </apex:column>  
      <apex:column value="{!acc.ArticleType}"/>  
     <apex:column value="{!acc.summary}"/>  
      <apex:column value="{!acc.UrlName}"/>  
      <apex:column value="{!acc.ArticleNumber}"/>  
    </apex:pageblocktable>  
   </apex:pageblock>  
  </apex:form>  
 </apex:page>  

Controller:
 public class articleSearchController {  
      //Search String used in ArticleList tag  
       public String searchstring { get; set; }  
      //Search Results List  
       Public List<KnowledgeArticleVersion> articleList{get;set;}  
       Public void getSearchResults()  
       {  
           String newSearchText = '%'+searchstring+'%';  
           system.debug('inputtext-->'+searchstring );  
           articleList= [SELECT Id,Title,UrlName,Summary,KnowledgeArticleId,ArticleType,ArticleNumber FROM KnowledgeArticleVersion WHERE (Language ='en_US' AND PublishStatus ='Online' AND Title LIKE : newSearchText) ];  
        system.debug('articleList-->'+articleList.size());   
   }  
 }  

Post a Comment

If you have any doubts, Please comment below and I will respond to you.

Previous Post Next Post