+ Start a Discussion
newbee developernewbee developer 

Salesforce knowledge articles - SOSL query to search entire object and display results in VF Page.

Hello All,

I am working on custom visual force page wherein there's an input box for user to enter some text.
Clicking on "Search" command button, will search in entire knowledge object and return the relevant results. 

Below is my code in VF Page:
<apex:form>
<apex:pageBlockSection title="Search" columns="1">
   <apex:inputText value="{!searchstring}" id="theSearchstring" maxlength="100" size="110"/>
   <apex:commandButton value="Search Article" id="submitButton" style="width:30" reRender="theSearchResults" action="{!searchKBDB}">
   <apex:param name="searchIAS" value="{!searchstring}" />
   </apex:commandButton>
   <apex:panelGroup id="theSearchResults" >
    <knowledge:articleList articleVar="article" Keyword="{!searchstring}">
    <apex:pageBlockTable value="{! SearchArticleList }" rendered="{!IF(searchstring!='',true,false)}" var="a" html-cid="searchTable">
     <apex:column value="{!a.Title}" headerValue="Article Number"/>
     <apex:column value="{!a.PublishStatus}" headerValue="Status"/>
     </apex:pageBlockTable>
    </knowledge:articleList>
     </apex:panelGroup>
     </apex:pageBlockSection>
  </apex:pageBlock>
</apex:form>
Below is my code in Apex Class(Controller) 
public  List<List<sObject>> SearchArticleList     {get;set;}

public PageReference searchKBDB() {
string strX = ApexPages.currentPage().getParameters().get('searchIAS');
 SearchArticleList     = [FIND strX RETURNING Knowledge__Kav(Id, Title, Summary, PublishStatus WHERE PublishStatus='online')];
return null;
}
This code is not working as expected. 

1. I am passing the search text to controller with parameter name as searchIAS.
2. I am using SOSL to search entire knowledge object and fetch results - Bind this to blocktable using the property SearchArticleList

Please suggest where I am goiung wrong.
Instead of SOSL query, should I use SOQL query for such scenarios? I need to perform search against entire knowledge object(for whatever fields are searchable). 

Thanks in advance for any help!!!
Best Answer chosen by newbee developer
NagendraNagendra (Salesforce Developers) 
Hi,

So there are two solutions that I can see you using for this. First is to add the values from the SOSL query to a wrapper class that you reference on the page. Second is to try using the .get() method instead of calling the name of the field.

So for the wrapper, it would look something like this.

String value = 'test'; List wrapperList = new List();
for(List<sObject> oList :[FIND :value RETURNING Account(Id, Name, Industry)]){
    for(sObject o: oList){
        Wrapper wrap = new Wrapper();
        wrap.field1 = o.get('Id');
        wrap.field2 = o.get('Name');
        wrapperList.add(wrap);
    }
}

public class Wrapper{
  public string field1 {get; set;}
  public string field2 {get; set;}

  public Wrapper(){
    field1 = '';
    field2 = '';
  }
}
In that code, you can see the .get() method that you might want to try to use instead of just calling the field name. Since not all objects could have the same field calling that specific field while referencing a sObject won't work.

Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra