Redirecting same page along with Success Message
Add this Code in Constructor
String message = '' + ApexPages.CurrentPage().GetParameters().Get('message');
String pageHeaderReferer = ApexPages.currentPage().getHeaders().get('Referer');
// Use the referrer parameter to only show the message when coming from Page 1
if(pageHeaderReferer != null && pageHeaderReferer.containsIgnoreCase('PageName') && message != 'null')
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, message));
}
This code add in save method
PageReference pg = new PageReference('/apex/PageName');
pg.getParameters().put('message', 'Your Message here');
pg.setRedirect(true);
return pg;
If you're redirecting to the same page, then just return null for the PageReference. Also, if you have any rerender logic, verify that the apex:pageMessages tag is rerendered.
What your action may look like in Apex:
public PageReference myAction(){
try{
insert CasePOs;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
}catch(DMLException e){
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Record creation failed: ' + e.getMessage()));
}
return null;
}