+ Start a Discussion
veeru_pariveeru_pari 

Creating FAQ custom object and searching FAQ's

Hi,
My task is how to store FAQ's in a custom object.From the the custom oject i need to display it on vf page.in vf page i need to have two radio buttons like internal and external(assume user are of type internal and external),if we select internal page should display internal FAQs and i f i select external it should display external FAQ's. And also i need to have a searchbox on the page and it should return the FAQs based on search criteria based on keys and tags.


please help me on this
Sonam_SFDCSonam_SFDC
Hi,

I would first suggest you to create a picklist on custom object FAQ to store if the FAQ is internal or external.

Once this is created you can then create a VF page with a field using selectoption/selectlist to be able to filter the records to see if they are internal or external.

Go to the following linkt o see how yu can implement search on records depending on the keywords entered:
http://vijaynalam09.blogspot.in/2012/08/contacts-search-through-visualforce-page.html
https://developer.salesforce.com/forums/ForumsMain?id=906F000000097u4IAA
veeru_pariveeru_pari
Hi Sonam,
i have search code below  where i can search a question with its beginning letter or if i give full question but i want to get the question even  if i given any letter  or word in middile of that question. please help me on this .I hope there might be wrong in like condition.Can i please know how can i solve this?

Here is the code for that
Vf page
<apex:page controller="QuesSearchCont" sidebar="false">
<apex:form >
  <apex:pageMessages id="errors" />

  <apex:pageBlock title="Find Me A Question & Answer!" mode="edit">

  <table width="100%" border="0">
  <tr>
    <td width="200" valign="top">

      <apex:pageBlock title="Parameters" mode="edit" id="criteria">

      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("question").value,
          document.getElementById("answer").value);
      }
      </script>

      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Question__c" value="" />
          <apex:param name="Answer__c" value="" />
        
        
      </apex:actionFunction>

      <table cellpadding="0" cellspacing="0">
      <tr>
        <td style="font-weight:bold;">QUESTION<br/>
        <input type="text" id="question" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">ANSWER<br/>
       <input type="text" id="answer" onkeyup="doSearch();"/>
        </td>
      </tr>
            </table>

      </apex:pageBlock>

    </td>
    <td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!faq}" var="faqs">

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Question" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="question" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
              <apex:outputField value="{!faqs.Question__c}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Answer" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="answer" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
               <apex:outputField value="{!faqs.Answer__c}"/>
            </apex:column>
        </apex:pageBlockTable>

    </apex:pageBlock>

    </td>
  </tr>
  </table>

  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />         
  </apex:pageBlock>  

  </apex:pageBlock>

  </apex:form>
</apex:page>

Controler:public with sharing class QuesSearchCont {
// the soql without the order and limit
private String soql {get;set;}
  // the collection of contacts to display
  public List<FAQ__c> faq {get;set;}

  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }

  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Question__c'; } return sortField;  }
    set;
  }

  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }

  // init the controller and display some sample data when the page loads
  public QuesSearchCont() {
    soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
    runQuery();
  }

  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }

  // runs the actual query
  public void runQuery() {

    try {
      faq = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessages(e);
    }

  }

  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
try{
FAQ__c f=new FAQ__c();
    String question = Apexpages.currentPage().getParameters().get('Question__c');
    String answer = Apexpages.currentPage().getParameters().get('Answer__c');
  
 
   
    soql = 'select Question__c, Answer__c from FAQ__c where Question__c!= null';
    if (!question.equals(''))
      soql += ' and Question__c LIKE \''+String.escapeSingleQuotes(question)+'%\'';
    if (!answer.equals(''))
      soql += ' and Answer__c LIKE \''+String.escapeSingleQuotes(answer)+'%\'';
   // if (!accountName.equals(''))
     // soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';
   

    // run the query again
    }
     catch(NullPointerException ne){
     ApexPages.addMessages(ne);
    }
    runQuery();

    return null;
    }
 


}