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
Carter85Carter85 

Need Help Displaying Apex Error Message on VF Page

I am trying to adjust my search page so that when a user search returns no results a message is displayed stating such.  However, at the moment while the search results for records that do exist display correctly, the error message does not show up when no matching records are found.  I would appreciate any help anyone could offer getting it fixed and have included my code snippets below.

Controller:

public with sharing class searchTestController {
 
  private ApexPages.StandardController controller {get; set;}
  public List<MG_Contract_Holder__c> searchResults {get;set;}
  public string searchText {get;set;}
 
  // Standard controller
  public searchTestController(ApexPages.StandardController controller) { }
 
  // Search button clicked
  public PageReference search() {
    String qry = 'select Vin_Number__c, Name, First_Name__c, Vehicle_Make__c, Vehicle_Model__c, Vehicle_Year__c, Product_Group__c, Effective_Date__c, Expiration_Date__c, Contract_Status__c, Contract_Number__c From MG_Contract_Holder__c ' +
    'where Vin_Number__c =:searchText order by name';  
    searchResults = Database.query(qry);
    
    if(searchResults == null){
    ApexPages.Message errormessage = new ApexPages.Message(ApexPages.Severity.ERROR,'No Records found.');
    ApexPages.addMessage(errormessage);
    }
    return null;
  }
}

 VF Page Search Form:

 <apex:form >
    <apex:pageBlock mode="detail" id="block">
       
      <apex:pageBlockSection >
      <apex:outputLabel for="searchText">Find a Customer - Enter a Vin Number:</apex:outputLabel>
          <apex:panelGroup >
          <apex:inputText id="searchText" value="{!searchText}" maxlength="17"/>
          </apex:panelGroup>
        <apex:commandButton value="Search" action="{!search}" rerender="block" status="status"/>
        <apex:pageBlockSectionItem >
        <apex:messages />
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection><br/>
 
      <apex:actionStatus id="status" startText="Searching... please wait..."/>
      <apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
          <apex:pageBlockTable value="{!searchResults}" var="item" rendered="{!NOT(ISNULL(searchResults))}">
          <apex:column style="text-align:center;" value="{!item.Name}" headerValue="Last Name" width="85"/>
          <apex:column style="text-align:center;" value="{!item.First_Name__c}" headerValue="First Name" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Contract_Number__c}" headerValue="Contract #" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Make__c}" headerValue="Make" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Model__c}" headerValue="Model" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Year__c}" headerValue="Year" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Product_Group__c}" headerValue="Product" width="85"/>         
          <apex:column style="text-align:center;" value="{!item.Effective_Date__c}" headerValue="Effective Date" width="85"/> 
          <apex:column style="text-align:center;" value="{!item.Expiration_Date__c}" headerValue="Expiration Date" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Contract_Status__c}" headerValue="Status" width="85"/> 
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>

 

Suresh RaghuramSuresh Raghuram

the first thing with VF Page

 

<apex:message id="msg"> you shoud give an Id to the Message tag.

 

Put you acual code in try block and message part in catch block. it will work

public with sharing class searchTestController {
 
  private ApexPages.StandardController controller {get; set;}
  public List<MG_Contract_Holder__c> searchResults {get;set;}
  public string searchText {get;set;}
 
  // Standard controller
  public searchTestController(ApexPages.StandardController controller) { }
 
  // Search button clicked
  public PageReference search() {
   
try{

String qry = 'select Vin_Number__c, Name, First_Name__c, Vehicle_Make__c, Vehicle_Model__c, Vehicle_Year__c, Product_Group__c, Effective_Date__c, Expiration_Date__c, Contract_Status__c, Contract_Number__c From MG_Contract_Holder__c ' + 'where Vin_Number__c LIKE \'%'+searchText+'%\' order by name'; searchResults = Database.query(qry); }Catch(Exception e){ if(searchResults == null){ ApexPages.Message(new ApexPages.Message(ApexPages.Severity.ERROR,'No Records found.'+e.getMessage()); } return null; } }

If this answers your question make this as asolution
I
Carter85Carter85

Thanks for the response.  However, it does not seem to be working.  It seems to find particular fault with this line:

ApexPages.Message(new ApexPages.Message(ApexPages.Severity.ERROR,'No Records found.'+e.getMessage()));

 It creates a compile error when I try to save:

Error: Compile Error: Method does not exist or incorrect signature: ApexPages.Message(ApexPages.Message) at line 19 column 5

 Is there any kind of accomodation I need to make for it to accept this syntax rather than the way I was trying to do it?

JFraileJFraile

Hi

 

Try adding this <apex:pageMessages rendered="true"/> to your VF instead of <apex:Messages/>

Carter85Carter85

Apologies, that does not seem to resolve it either.  It it my placement of the elements within the page perhaps?  I still can't get the error message to display trying to apply these methods.

Suresh RaghuramSuresh Raghuram
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error while updating the record : '+e.getMessage())); 



The above is the right syntax make use of it it will work.

Make this as solution if this answers your question
Carter85Carter85

I don't know what I'm still missing, because I think you're right, it should be working the way you've suggested.  I must be assigning the Id improperly or have created some sort of error in my tinkering because it still does not display the message if no result is returned.  The updated code is below for each component in case I did something to negate your intended fix and just keep missing it.

Controller:

public with sharing class searchTestController {
 
  private ApexPages.StandardController controller {get; set;}
  public List<MG_Contract_Holder__c> searchResults {get;set;}
  public string searchText {get;set;}
 
  // Standard controller
  public searchTestController(ApexPages.StandardController controller) { }
 
  // Search button clicked
  public PageReference search() {
    try{
    String qry = 'select Vin_Number__c, Name, First_Name__c, Vehicle_Make__c, Vehicle_Model__c, Vehicle_Year__c, Product_Group__c, Effective_Date__c, Expiration_Date__c, Contract_Status__c, Contract_Number__c From MG_Contract_Holder__c ' +
    'where Vin_Number__c =:searchText order by name';  
    searchResults = Database.query(qry);
    }
    catch(Exception e){    
    if(searchResults == null){ 
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error, No Records Found: '+e.getMessage()));
    }
    }
    return null;
  }
}

 VF Form:

 <apex:form >
    <apex:pageBlock mode="detail" id="block">
       
      <apex:pageBlockSection >
      <apex:outputLabel for="searchText">Find a Customer - Enter a Vin Number:</apex:outputLabel>
          <apex:panelGroup >
          <apex:inputText id="searchText" value="{!searchText}" maxlength="17"/>
          </apex:panelGroup>
        <apex:commandButton value="Search" action="{!search}" rerender="block, errorMessage" status="status"/>
        
      </apex:pageBlockSection><br/>
 
      <apex:actionStatus id="status" startText="Searching...please wait..."/>
      <apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
          
          <apex:pageBlockSectionItem >
        <apex:pageMessages id="errorMessage" ></apex:pageMessages>
        </apex:pageBlockSectionItem>
          
          <apex:pageBlockTable value="{!searchResults}" var="item" rendered="{!NOT(ISNULL(searchResults))}">
          <apex:column style="text-align:center;" value="{!item.Name}" headerValue="Last Name" width="95"/>
          <apex:column style="text-align:center;" value="{!item.First_Name__c}" headerValue="First Name" width="95"/>
          <apex:column style="text-align:center;" value="{!item.Contract_Number__c}" headerValue="Contract #" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Make__c}" headerValue="Make" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Model__c}" headerValue="Model" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Vehicle_Year__c}" headerValue="Year" width="60"/>
          <apex:column style="text-align:center;" value="{!item.Product_Group__c}" headerValue="Product" width="60"/>         
          <apex:column style="text-align:center;" value="{!item.Effective_Date__c}" headerValue="Effective Date" width="85"/> 
          <apex:column style="text-align:center;" value="{!item.Expiration_Date__c}" headerValue="Expiration Date" width="85"/>
          <apex:column style="text-align:center;" value="{!item.Contract_Status__c}" headerValue="Status" width="85"/> 
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>