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
Preethi SPreethi S 

how to display success message in visualforce page by clicking save button

hi all,

I created a visualforce page to insert record....then If i click save button that will show success message in the same visualforce page...

this is my visualforce page

<apex:page standardController="Candidate__c" extensions="SuccessController1">
<apex:form >
<apex:pageBlock title="CandidateInformation" mode="edit">
<apex:pageBlockSection title="Information" columns="2">
<apex:inputField value="{!Candidate__c.FirstName__c}"/>
<apex:inputField value="{!Candidate__c.LastName__c}"/>
<apex:inputField value="{!Candidate__c.SSN__c}"/>
<apex:inputField value="{!Candidate__c.city__c}"/>
<apex:inputField value="{!Candidate__c.Phone__c}"/>
<apex:inputField value="{!Candidate__c.Email__c}"/>
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

I created one controller to call the save method.

public with sharing class SuccessController1 {

    Candidate__c candidate = new Candidate__c();
    public SuccessController1(ApexPages.StandardController controller)
    {
 
     }
  
    public PageReference save() {

      insert candidate;
PageReference pg = new PageReference('/apex/candidate');
pg.setRedirect(true);
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
return pg;
   }
   }


But the record is not inserted. it shows insertion failed error message.. can u help me for this?
Doug BjoinDoug Bjoin
 

 

I am still pretty new to VF, but it appears you are trying to insert a blank record. I think you need to get the record from the standard controller. In the extension constructor, try getting the record. Example.

 

 

public with sharing class SuccessController1 {

 private Apexpages.Standardcontroller controller;
 private final Candidate__c candidate;
   
 
 public SuccessController1(ApexPages.StandardController controller)
    {
  this.candidate = (Candidate__c)stdController.getRecord();
    }
 
    public PageReference save() {

      stdController.save();
      ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
      return null;
   }
}

 

 

 

Preethi SPreethi S
hi,

this is not working. I changed my controller and visualforce like that...

public with sharing class SuccessController1 {

    public SuccessController1() {

    }
Candidate__c cand = new Candidate__c();
Public Candidate__c getCand()
  {
  
   return Cand;
   }
  public SuccessController1(ApexPages.StandardController controller)
    {
 
      }
  
    public PageReference save() {

      insert cand;
PageReference pg = new PageReference('/apex/candidate');
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
pg.setRedirect(true);
return pg;
   }
   }


visualforce page:

<apex:page controller="SuccessController1">
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="CandidateInformation" mode="edit">
<apex:pageBlockSection title="Information" columns="2">
<apex:inputField value="{!cand.FirstName__c}"/>
<apex:inputField value="{!cand.LastName__c}"/>
<apex:inputField value="{!cand.SSN__c}"/>
<apex:inputField value="{!cand.city__c}"/>
<apex:inputField value="{!cand.Phone__c}"/>
<apex:inputField value="{!cand.Email__c}"/>
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


 my record is inserted now.. but i dont know how to show only  success message in same vf page... can u help for this?
Doug BjoinDoug Bjoin
    public PageReference save() {

      insert cand;
     ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
      return null;
   }
Preethi SPreethi S
Its not working... But I got a idea for how to hide fields in vf page...we have to use "rendered" attribute in vf page..then we have to declare  boolean variable in controller...This is my vf page

<apex:page controller="SuccessController1" showheader="false">
<apex:form id="file1">
<apex:pageMessages id="msg"/>
<apex:pageBlock title="CandidateInformation" mode="edit">
<apex:pageBlockSection title="Information" columns="2">
<apex:inputField value="{!cand.FirstName__c}"/>

<apex:inputField value="{!cand.LastName__c}" />


<apex:inputField value="{!cand.SSN__c}" />


<apex:inputField value="{!cand.city__c}" />


<apex:inputField value="{!cand.Phone__c}"  />
<apex:inputField value="{!cand.Email__c}"  />


<apex:commandButton action="{!save}" value="save"/>
<apex:outputPanel id="file1" rendered="{!hidefn}">
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>

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

controller:

public with sharing class SuccessController1 {
public boolean hidefn { get; set; }
    public SuccessController1() {

hidefn=false;
    }
Candidate__c cand = new Candidate__c();
Public Candidate__c getCand()
  {
  
   return Cand;
   }
  public SuccessController1(ApexPages.StandardController controller)
    {
 
      }
      public PageReference save()
      {
      insert cand;
      PageReference p = apexPages.currentPage();
      ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!');
      ApexPages.addMessage(msg);
      return p;
      }
    }

But I don't know where we have to use that "rendered "attribute in vf page...after  clicking save button i want to show  success message with blank page...
 If i use that rendered attribute in input field.....that field will be hided before giving input...Anyone can help for this..