function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fitzgerald AnukamFitzgerald Anukam 

Help!! "Apexpages.addMessage" Not Displaying On VisualForce Page With Search Field

Hi, I have created a VF page for my custom object with a search field included. The search field works fine but when I enters a null character or 1 character, I expects it to show me an error message in the VF page, ideally in .INFO style but it is showing me an expression error on another page.

User-added image

I am very new to VisualForce and Apex and would appreciate some help. I know I am miissing something out but can't seem to figure it out.

Apex Class:
public class QFSearchController {

    String searchText;
    List<Quality_Feedback__c> results;
    

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String s) {
        searchText = s;
    }
    
    public List<Quality_Feedback__c> getResults() {
        return results;
    }

    public PageReference doSearch() {
    

if(searchText==null || searchText=='')
 { 
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Search term must be longer than one character:')); 
 }
 
 else if (searchText.length()<2)
 {
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Input box must contain at least two characters')); 
 }
 results = (List<Quality_Feedback__c>)[FIND :searchText RETURNING Quality_Feedback__c(Name, Type_of_Feedback__c,    
Feedback_For__c,   Reviewed_By__c, Review_Status__c, Incident__c)][0];
        return null;
    }
}

VisualForce Page:
<apex:page controller="QFSearchController"> 
 <apex:ListViews type="Quality_Feedback__c" />
   <apex:form >   
      <apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}" 
                                      rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l" 
                               rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.name}"/>
              <apex:column value="{!l.Type_of_Feedback__c}"/>
              <apex:column value="{!l.Feedback_For__c}"/>
              <apex:column value="{!l.Reviewed_By__c}"/>
              <apex:column value="{!l.Review_Status__c}"/>
              <apex:column value="{!l.Incident__c}"/>
           </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
       <apex:pageMessages />
   </apex:form>
 
</apex:page>

Thanks in advance.
Best Answer chosen by Fitzgerald Anukam
Shashikant SharmaShashikant Sharma
Hi,

I noticed that your code is making a SOSL even if there are not enough charachters.

Try with these changes in your controller
 
public class QFSearchController {

    String searchText;
    List<Quality_Feedback__c> results;
    

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String s) {
        searchText = s;
    }
    
    public List<Quality_Feedback__c> getResults() {
        return results;
    }

    public PageReference doSearch() {
// reinitialize the results list before making search
results = new List<Quality_Feedback__c>();
if(searchText==null || searchText=='')
 { 
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Search term must be longer than one character:')); 
 }
 else if (searchText.length()<2)
 {
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Input box must contain at least two characters')); 
 }
// only query in case of enough input
else if { 
results = (List<Quality_Feedback__c>)[FIND :searchText RETURNING Quality_Feedback__c(Name, Type_of_Feedback__c,    
Feedback_For__c,   Reviewed_By__c, Review_Status__c, Incident__c)][0];
}
 
       return null;
    }
}

I hope that should work.

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi,

I noticed that your code is making a SOSL even if there are not enough charachters.

Try with these changes in your controller
 
public class QFSearchController {

    String searchText;
    List<Quality_Feedback__c> results;
    

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String s) {
        searchText = s;
    }
    
    public List<Quality_Feedback__c> getResults() {
        return results;
    }

    public PageReference doSearch() {
// reinitialize the results list before making search
results = new List<Quality_Feedback__c>();
if(searchText==null || searchText=='')
 { 
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Search term must be longer than one character:')); 
 }
 else if (searchText.length()<2)
 {
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Input box must contain at least two characters')); 
 }
// only query in case of enough input
else if { 
results = (List<Quality_Feedback__c>)[FIND :searchText RETURNING Quality_Feedback__c(Name, Type_of_Feedback__c,    
Feedback_For__c,   Reviewed_By__c, Review_Status__c, Incident__c)][0];
}
 
       return null;
    }
}

I hope that should work.

Thanks
Shashikant
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
pageMessages was not coming because you rerender the block section only and you added pageMessages outside of block section

Please try below VF page.
<apex:page controller="QFSearchController"> 
 <apex:ListViews type="Quality_Feedback__c" />
   <apex:form >   
      <apex:pageBlock mode="edit" id="block">
		 <apex:pageMessages />
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}" 
                                      rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l" 
                               rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.name}"/>
              <apex:column value="{!l.Type_of_Feedback__c}"/>
              <apex:column value="{!l.Feedback_For__c}"/>
              <apex:column value="{!l.Reviewed_By__c}"/>
              <apex:column value="{!l.Review_Status__c}"/>
              <apex:column value="{!l.Incident__c}"/>
           </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
 
</apex:page>


Let us know if this will help you

Thanks
Amit Chaudhary
Fitzgerald AnukamFitzgerald Anukam
Thanks Shashikant, your answer pointed me to the right direction and stopped it from showing on another page like it was before and I saw what I was missing. Also in the container (doSearch), the statement should end with "else" and not "else if". 
 
public class QFSearchController {

    String searchText;
    List<Quality_Feedback__c> results;
    

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String s) {
        searchText = s;
    }
    
    public List<Quality_Feedback__c> getResults() {
        return results;
    }


    public PageReference doSearch() {
    
// reinitialize the results list before making search
results = new List<Quality_Feedback__c>();

if(searchText==null || searchText=='')
 { 
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Search term must be longer than one character:')); 
 }
 else if (searchText.length()<2)
 {
 Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Input box must contain at least two characters')); 
 }
 
// only query in case of enough input
else {

results = (List<Quality_Feedback__c>)[FIND :searchText RETURNING Quality_Feedback__c(Name, Type_of_Feedback__c,    
Feedback_For__c,   Reviewed_By__c, Review_Status__c, Incident__c)][0];

}
 
       return null;
    }
}

Thanks Amit, yes putting the <apex:pageMessages />  outside the block will stop it from displaying but won't prevent it from showing up on another page like it was.

Regardless, thanks guys I appreciate your help.