+ Start a Discussion
Nicolás KacowiczNicolás Kacowicz 

reload visualforce page after displaying custom message

Hello, I've got a Visualforce Page that is just a form with a Save button.
I'm trying to display a message if the record is inserted correctly or not, and if it is inserted correctly I want to reload the page and delete every value stored in the form.
I have this Apex Code:

public PageReference save() {
        try {
            upsert accRecord;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Record submited successfully'));
        } catch(System.DMLException e) {
            //ApexPages.addMessages(e);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account Name'));
            return null;
        }
        //  After successful Save, navigate to the default view page        
        PageReference tempPage = ApexPages.currentPage(); 
        tempPage.setRedirect(true);
        return tempPage;
        //return null;
    }

Right now it doesn't show the message, but if I delete the PageReference tempPage variable and leave 'return null' it will show the message and but stay in the same page with the values of the inserted record.

Thanks!
pradeep kumar yadavpradeep kumar yadav
You can do "tempPage" portion of code on VF page script
as shown below
<apex:page>
<a href="" id="ancherId" target="_blank"></a>
        <script type="text/javascript">
            var msg = document.getElementById("pgMessageID").value;
                if(msg.contains('successfully')) {
                        document.getElementById("ancherId").setAttribute('target', '_self');
                        document.getElementById("ancherId").href = '/apex/VFpageName';
                        document.getElementById("ancherId").click();
                    }
       </script>
 <apex:pageMessage id="pgMessageID" />
</apex:page>

Or you can simply reload the page by checking pageMessage in javascript and call "location.reload"