+ Start a Discussion
satakshisatakshi 

How to add success message in code?

Hello,

I want to add success message in code. when i will create a visit. after creating visit success msg should be displayed.

Here I am  Posting my code:

Controller:


public with sharing class MyCompController3

public  id tid{get;set;}
public String str;
public String city{get;set;}
    public MyCompController3(ApexPages.StandardController controller)
     {
 
     }
    
    public pagereference dosome(){
    
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, RecordType.Name, Name, City__c from Account where City__c =:city])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}

    return null;
    }

public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--None--','--None--'));
            options.add(new SelectOption('Ahmednagar','Ahmednagar'));
            options.add(new SelectOption('Akola','Akola'));
            options.add(new SelectOption('Amravati','Amravati'));
            options.add(new SelectOption('Aurangabad','Aurangabad'));
            options.add(new SelectOption('Bhiwandi','Bhiwandi'));
            options.add(new SelectOption('Chandrapur','Chandrapur'));
            options.add(new SelectOption('Chinchwad','Chinchwad'));
            options.add(new SelectOption('Dhule','Dhule'));
            options.add(new SelectOption('Dombivali','Dombivali'));
            options.add(new SelectOption('Jalgaon','Jalgaon'));
            options.add(new SelectOption('Kalyan','Kalyan'));
            options.add(new SelectOption('Kolhapur','Kolhapur'));
            options.add(new SelectOption('Latur','Latur'));
            options.add(new SelectOption('Malegaon','Malegaon'));
            options.add(new SelectOption('Mumbai','Mumbai'));
            options.add(new SelectOption('Nagpur','Nagpur'));
            options.add(new SelectOption('Nanded','Nanded'));
            options.add(new SelectOption('Nashik','Nashik'));
            options.add(new SelectOption('Navi Mumbai','Navi Mumbai'));
            options.add(new SelectOption('Parbhani','Parbhani'));
            options.add(new SelectOption('Pimpri','Pimpri'));
            options.add(new SelectOption('Pune','Pune'));
            options.add(new SelectOption('Sangli','Sangli'));
            options.add(new SelectOption('Satara','Satara'));
            options.add(new SelectOption('Solapur','Solapur'));
            options.add(new SelectOption('Thane','Thane'));
            options.add(new SelectOption('Vasai','Vasai'));
            options.add(new SelectOption('Virar','Virar'));

            return options;
        }


public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}

private List<Id> accountids=new list<Id>();
public List<Account> acc;

public PageReference processSelected()
{
    selectedAccount = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapAccountList)
    {
    if(wrapAccountObj.selected == true)
    {
        selectedAccount.add(wrapAccountObj.acc);
        //accWrap.acc.Name
        system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);

    }
    }
             PageReference pageRef = new PageReference('/apex/Visitplan');
             return pageRef;
            
}

public list<Account> srecs;
public list<Account> getSrecs()
{


 
 srecs= [select Id, Name, RecordType.Name, City__c from Account];
 system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);
 tid = ApexPages.currentPage().getParameters().get('id');
 system.debug('*****************!!!!!!!!!$$$$$$$$$$$$$$$$$$$$$$$$$******************'+tid);

        return selectedAccount;
}
public PageReference CreateRec()
{
for(Account  acr :selectedAccount)
{
Planned_Visit__c pv =new Planned_Visit__c();
pv.Daily_Visit_Plan__c= tid;
pv.Name__c=acr.id;
insert pv;
}
             return null;
}

public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}

public wrapAccount(Account a)
 {
acc = a;
selected = false;
}
}
}



VF page 1:

<apex:page standardController="Daily_Visit_Plan__c" extensions="MyCompController3" standardStylesheets="false" sidebar="false">

<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>

<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Show" action="{!dosome}" reRender="table"/>
<apex:commandButton value="Show Selected" action="{!processSelected}" />
</apex:pageBlockButtons>

<apex:actionRegion >
<apex:selectList value="{!city}" size="1">
<apex:selectOptions value="{!items}"/>
<!--<apex:actionSupport event="onchange" reRender="table" action="{!dosome}" />-->
</apex:selectList>
</apex:actionRegion>
        

<apex:pageblockSection title="All Doctors And Chemists" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists" id="table">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.City__c}" />
<apex:column value="{!accWrap.acc.RecordType.Name}" />
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

<!--<apex:page standardController="Daily_Visit_Plan__c" extensions="MyCompController" standardStylesheets="false" sidebar="false">

<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
}
}
}
</script>

<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Create Visit" action="{!processSelected}" />
</apex:pageBlockButtons>

<apex:pageblockSection title="All Contacts" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />

</apex:pageBlockTable>

</apex:pageblockSection>

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

</apex:page>-->


VF page 2:



<apex:page standardController="Daily_Visit_Plan__c" extensions="MyCompController3" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Displaying selected list of Doctors And Chemist.." collapsible="false">
<apex:pageBlockTable value="{!srecs}" var="item" >
<!--<apex:column value="{!item.id}" />-->
<apex:column value="{!item.name}" />
<apex:column value="{!item.City__c}" />
<apex:column value="{!item.RecordType.Name}" />
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Create Visit" action="{!CreateRec}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Best Answer chosen by satakshi
Dayakar.DDayakar.D
Hi Satakshi,

use below statement after insert statement.
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully inserted visit.'));

and add below tag in form tag.
<apex:pageMessages ></apex:pageMessages>

like this.
public PageReference CreateRec()
    {
        for(Account  acr :selectedAccount)
        {
            Planned_Visit__c pv =new Planned_Visit__c();
            pv.Daily_Visit_Plan__c= tid;
            pv.Name__c=acr.id;
            insert pv;
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully inserted visit.'));
        }
        return null;
    }

<apex:form >
        <apex:pageMessages ></apex:pageMessages>

It will display message on your vf page.

Please let me know, if it helps you.

Best Regards,
Dayakar.D
 

All Answers

Dayakar.DDayakar.D
Hi Satakshi,

use below statement after insert statement.
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully inserted visit.'));

and add below tag in form tag.
<apex:pageMessages ></apex:pageMessages>

like this.
public PageReference CreateRec()
    {
        for(Account  acr :selectedAccount)
        {
            Planned_Visit__c pv =new Planned_Visit__c();
            pv.Daily_Visit_Plan__c= tid;
            pv.Name__c=acr.id;
            insert pv;
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully inserted visit.'));
        }
        return null;
    }

<apex:form >
        <apex:pageMessages ></apex:pageMessages>

It will display message on your vf page.

Please let me know, if it helps you.

Best Regards,
Dayakar.D
 
This was selected as the best answer
satakshisatakshi
Hey Dayakar,

  Thanks a lot. Its working :-))

Regards,
Satakshi