+ Start a Discussion
veeru_pariveeru_pari 

posting questions

Hi,
I have appliction where i am having q&a stored in custom object when i create a new record as of now i am able to searh then in a vf page. Now my req is i need to post the ques from fron end ie from vf page .As of now i am posting it in backend that is in Custom obj.So please let me know the code how to post the ques from front end?


The code i have written is as follows

VFpage:
<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>
Controller:
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;
    }
  
 

}
The custom obj name is FAQ having fields question,answe and users.


Thanks
Veeraiah

Ashish_SFDCAshish_SFDC
Hi Veeraaiah, 


Need some more information on this, 

Are you getting any errors? Did you try the debuig logs?

As we try to display Create record form on the Visualforce you have to link it to the Custom Field which will store the Question. 

See the below thread for code to 'Insert Record into custom object'

https://developer.salesforce.com/forums/ForumsMain?id=906F000000092Z0IAI


Regards,
Ashish