You need to sign in to do that
Don't have an account?
Carter85
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>
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
Thanks for the response. However, it does not seem to be working. It seems to find particular fault with this line:
It creates a compile error when I try to save:
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?
Hi
Try adding this <apex:pageMessages rendered="true"/> to your VF instead of <apex:Messages/>
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.
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:
VF Form: