September 17, 2018

How to display a success message on Visualforce page when button is clicked?

Sample Code:

Visualforce page:

  1. <apex:page controller="Sample">  
  2.       
  3.     <apex:form >  
  4.       
  5.         <apex:pageBlock >  
  6.           
  7.             <apex:pageMessages />  
  8.           
  9.             Testing Page Message  
  10.               
  11.             <apex:pageBlockButtons location="bottom">  
  12.               
  13.                 <apex:commandButton value="Call" action="{!check}"/>  
  14.                   
  15.             </apex:pageBlockButtons>  
  16.               
  17.         </apex:pageBlock>  
  18.           
  19.     </apex:form>  
  20.       
  21. </apex:page>  

Apex Class:

  1. public class Sample {  
  2.   
  3.     public Sample() {  
  4.           
  5.     }  
  6.       
  7.     public void check() {  
  8.       
  9.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Check button is clicked'));  
  10.           
  11.     }  
  12.       
  13. }  

Output:


2 comments:

  1. Wonderful .! It was simple and easy to understand.
    Can you please also tell how to differentiate between success and error message in Visual force page.

    ReplyDelete
    Replies
    1. Check this for other Serverities - http://www.infallibletechie.com/2013/07/apexpagesseverity-supported-values.html

      Delete