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
SumaSuma 

Dynamic search with apex and visualforce. Need help in displaying error message in VF page.

I am trying to search Contacts details from database using lastname field and dsiplay the table in VF page.  I am able to search and display the existing records in the page but if the record(Contact) is not in the database, I need to display the error message: "Contact could not found".  This is where i am looking for some help.
Code for my controller is:

public with sharing class ContactSearchController
{
    // Global variables
         public List<Contact> contacts{get;set;}
         public String searchString{get;set;}
         
   // Constructor for stand alone page
   
            
         public ContactSearchController()
         {
          searchString = '';
          doSearch();
         }
         
    // Methods for getting List of Contacts
       
         public void doSearch()     
         {
            String queryString = '';
            
            if(searchString!= null)
            {
                queryString = '%'+searchString+'%';
            
                contacts = [SELECT id, name, accountId, email, phone 
                            FROM contact C
                            WHERE lastname LIKE :queryString
                            LIMIT 10];
            
           }
           else
           {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact could not found!')); 
                        
         }
        
 }        
}

My VF page is :
<apex:page controller="ContactSearchController" >
<apex:form >

<!-- actionFunction is for calling Controller's method -->
<apex:actionFunction action="{!doSearch}" name="apexdoSearch" reRender="myData,errors"/>

<!-- Filter Section -->
     <label> <b>Filter by Last Name:</b></label>
       <apex:inputText value="{!searchString}" onkeyup="apexdoSearch()"/>
       <br></BR>
       
  

 <!-- My Page Block -->
  <apex:pageBlock title="Find Customer" id="myData">
  <apex:pageMessages id="errors"/>
  <apex:pageBlockTable value="{!contacts}" var="contact">
  <apex:column value="{!contact.name}"/>
  <apex:column value="{!contact.accountid}"/>
  <apex:column value="{!contact.email}"/>
  <apex:column value="{!contact.phone}"/>
  </apex:pageBlockTable>
  </apex:pageBlock>
  </apex:form>
</apex:page>
Best Answer chosen by Suma
James LoghryJames Loghry
You are only adding an error message if the search string is null.  Instead, you should check whether or not your contact list is empty.  See my example below.  Also, you really should move any displayed text into custom labels for easy text changes / translations needed. It's just a good habit to get into.
if(searchString!= null){
    queryString = '%'+searchString+'%';
            
    contacts = [SELECT ..];

    if(contacts.isEmpty()){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Label.Contact_Not_Found));  
    }  
}else{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,Label.Search_String_Empty)); 
}

All Answers

James LoghryJames Loghry
You are only adding an error message if the search string is null.  Instead, you should check whether or not your contact list is empty.  See my example below.  Also, you really should move any displayed text into custom labels for easy text changes / translations needed. It's just a good habit to get into.
if(searchString!= null){
    queryString = '%'+searchString+'%';
            
    contacts = [SELECT ..];

    if(contacts.isEmpty()){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Label.Contact_Not_Found));  
    }  
}else{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,Label.Search_String_Empty)); 
}
This was selected as the best answer
SumaSuma
Thanks James ! It worked.  Also thanks for the tip of custom labels :)