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
akshay desai 9akshay desai 9 

How can I show success message on visulaforce Page

<apex:page standardController="Campaign" extensions="ForSaleLookupOnCampaignController"> 
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");                  
            for(var i=0; i<inputCheckBox.length; i++){          
                    inputCheckBox[i].checked = obj.checked;
    <apex:form >
        <apex:pageBlock >
             <apex:pageBlockButtons >
                <apex:commandButton value="Save" style="width: 100px; height: 30px" action="{!processSelected}" rerender="table2"/>
            <p style="font-size: 15px;font-weight: bold;">Select FID :</p><apex:inputfield style="width: 200px; height: 30px" value="{!Campaign.For_Sale__c}" id="forsaleid"/><br/>
            <apex:pageblockSection title="Related Campaign Members" collapsible="false">
                <apex:pageBlockTable value="{!wrapcontactList}" var="conWrap" id="table" title="All Related Campaign Members">     
                    <apex:column >
                        <!--apex:facet name="header">
                          <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        <apex:inputCheckbox value="{!conWrap.selected}" id="inputId"/>
                    <apex:column value="{!conWrap.con.Name}" />
                    <apex:column value="{!}" />
public class ForSaleLookupOnCampaignController { 
    public List<CampaignMember> Campaignmemberlist{get;set;} 
    public Campaign cmp {get;set;} 
    public List<wrapContact> wrapcontactList {get; set;}
    public List<Contact> selectedcontacts{get;set;}
    set<id>conid=new set<ID>();
    public String ID { get; set;}
    set<id>ForSaleid=new set<id>();
    public ForSaleLookupOnCampaignController (ApexPages.StandardController standardControllerParam) {
        this.cmp = (Campaign)standardControllerParam.getRecord();
        Campaignmemberlist = [SELECT id,name,Campaignid,Company__c,type,For_Sale__c,Contact.For_Sale__c  FROM CampaignMember WHERE Campaignid = : ApexPages.currentPage().getParameters().get('id') ];
        for(CampaignMember cm:Campaignmemberlist){
        if(wrapContactList == null) {
            wrapContactList = new List<wrapContact>();
            for(Contact c: [select Id,Name,,For_Sale__c from Contact where id=:conid]) {
                wrapContactList.add(new wrapContact(c));
    public PageReference processSelected() {
        PageReference pr;
        selectedcontacts = new List<contact>();
        system.debug('for sale '+cmp.For_Sale__c);
        for(wrapContact wrapcontactObj : wrapcontactList) {
            if(wrapcontactObj.selected == true) {
        for(contact s:selectedcontacts){
            }catch( Exception e ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'FID Update on Contact'));
      return pr;   
    public class wrapContact {
        public Contact con {get; set;}
        public string forsaleid{get;set;}
        public Boolean selected {get; set;}
        public wrapContact(Contact c) {
            con = c;
            selected = false;
How should I show success message on VF Page
& another thing is
here <apex:inputfield style="width: 200px; height: 30px" value="{!Campaign.For_Sale__c}" id="forsaleid"/> when I click on button & page is load the value is already populate on this but I want to clear this value when page populate how can I do that?

Best Answer chosen by akshay desai 9
Danish HodaDanish Hoda
Hi Akshay,
Please add <apex:pageMessages ></apex:pageMessages> on your VF page, it will show the messgaes you are setting ApexPages.addmessage()

All Answers

Danish HodaDanish Hoda
Hi Akshay,
Please add <apex:pageMessages ></apex:pageMessages> on your VF page, it will show the messgaes you are setting ApexPages.addmessage()
This was selected as the best answer
Hi akshay ,

Visualforce page:

<apex:page controller="Sample">  
    <apex:form >  
        <apex:pageBlock >  
            <apex:pageMessages />  
            <apex:pageBlockButtons location="bottom">  
                <apex:commandButton value="Show Success message" action="{!check}"/>  

Apex Class:

public class Sample {  
    public Sample() {  
    public void check() {  
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Check button is clicked'));  

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
akshay desai 9akshay desai 9
Thanks Danish but what if I want to show an error message