public PageReference Save() {

        if (cardRequest != NULL) {

            insert cardRequest;

            PageReference pr = new PageReference('/' + Business_Card_Request__c.getSObjectType().getDescribe().getKeyPrefix());


            return pr;


        return null;


Visualforce Page

        <apex:pageBlockButtons id="businessCardRequestButtonSetOne">
            <apex:commandButton value="SAVE" action="{!Save}"/>
            <apex:commandButton value="CANCEL" immediate="true" html-formnovalidate="formnovalidate" action="{!Cancel}"/>

I believe cardRequest is the culprit. When I click the save button on the Visualforce Page, the page will refresh and the record will not be saved.

  • Put a error handler and debug the save method, Mar 24, 2017 at 15:35
  • 1
    Does your save button have a rerender attribute? It is your markup we need to see here.
    – Adrian Larson
    Mar 24, 2017 at 15:40

1 Answer 1


When you have an <apex:commandButton> tag and you do not specify a rerender attribute, the action will refresh the entire page. In the case of error handling for an extension, you would want to rerender an <apex:pageMessages> tag (or <apex:messages>, as you wish). The following pattern is common:


public PageReference save()
    PageReference redirect;
    // set to whatever you wish
    // I'd normally set it in the controller's constructor

        insert record; // e.g. cardRequest
        // if you wanted to redirect to the record view, use:
        // redirect = new ApexPages.StandardController(record).view();
    catch (DmlException d)
        // now these error messages will show up in your pageMessages tag

        return null;
        // now you won't redirect anywhere
    return redirect;


<apex:page ...>
    <apex:pageMessages id="msgs" />

    <!-- intermediate markup -->
    <apex:commandButton action="{!save}" value="Save" rerender="msgs" />
    <!-- intermediate markup -->


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .