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
RakheebRakheeb 

Apex messgaes:i want to display error message on visualforce page ?

How can i display error message on visualforce page  and i have code i.e for searching name,phone and email in leads .if name already exist then it will populate appropirate name,email and phone ,if suppose name its not there so i want to display info message.Here i have code but i am not getting error message so kindly let me know the solution ASAP. 

Controller:

public class theController {

   String searchText;
   List<Lead> results;

   public String getSearchText() {
      return searchText;
   }

   public void setSearchText(String s) {
      searchText = s;
   System.debug('SEARCHTEXT:'+searcHText);
   }

   public List<Lead> getResults() {
     
     if(results == null)
      {
       ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');
      }
   return results;
   }

   public PageReference doSearch() {
    
     results = (List<Lead>)[FIND :searchText RETURNING Lead(Name, Email, Phone)][0];
           
      return null;
   }
}

Page:

 

<apex:page controller="theController">
<apex:messages />
   <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.email}"/>
              <apex:column value="{!l.phone}"/>
           </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
</apex:page>

goabhigogoabhigo

You are almost there. 

Modify like this:

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');

ApexPages.addMessage(myMsg);

-----OR------

ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg'));

 

You can also glance into : http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_message.htm

 

Hope it helps.

MeenakshmiMeenakshmi

Another way to show your message is:

 

 

public class Extension
{

//Declare a property in your apex class

public String msg{get;set;}

 

public void method()

{

//assign value to that property

msg=' Message detail';

}

}

 

<apex:page standardController="sObject"  extensions="Extension">

<apex:form >
<apex:pageBlock >
<apex:outputText  value="{!msg}"></apex:outputText>

</apex:pageBlock>
 </apex:form>
</apex:page>

 

Mudasir WaniMudasir Wani

Give some id to the <apex:messages /> and then rerender in the action you are calling.

For example 

<apex:messages id="myMessages"/>

 

and modify 

 <apex:commandButton value="Go!" action="{!doSearch}" rerender="block,myMessages" status="status"/>

 

This will solve the problem.