1

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.

1

After you upsert your record, reset it to an empty value, then rerender the page. All the field values stored on the page will pull empty values from the new record instead.

    try {
        upsert accRecord;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Record submited successfully'));

        accRecord = new Account(); 
        return null; 
    } catch(System.DMLException e) {
        //ApexPages.addMessages(e);
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account Name'));
        return null;
    }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.