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
etechcareersetechcareers 

How to return/display error message when no records are found???

Hi:

   When no records are found in my search result I need to return a message to the user on the page No records were found...Here is my code for it.. As you see I tried to do it but nuthing happens...Someone please help..

 

public List<Account> getResults() { 
     
return this.results;    
      
}    

public PageReference doSearch() {        
    results = (List<Account>)
    [FIND :searchText IN NAME FIELDS RETURNING Account(Name,BillingState,BillingCity,Phone,Website,Id,RecordType.Name)] [0];        
if(results==null){  
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'No Records found'));
    }

return null;    
}

 

 

waylonatcimwaylonatcim

Did you add a messages tag to the visualforce page?

 

<apex:messages />

 

 

I believe that is required for displaying the messages from controllers outside of the debug log.

gv007gv007

public PageReference doSearch() {       
    results = (List<Account>)
    [FIND :searchText IN NAME FIELDS RETURNING Account(Name,BillingState,BillingCity,Phone,Websit​e,Id,RecordType.Name)] [0];       
if(results==null){ 
ApexPages.Message errormessage = new ApexPages.Message(ApexPages.Severity.ERROR,'No Records found');
    ApexPages.addMessage(errormessage);
    }

return null

 

try this.