1

Controller

    public PageReference Save() {

        if (cardRequest != NULL) {

            insert cardRequest;

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

            pr.setRedirect(true);

            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}"/>
        </apex:pageBlockButtons>

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.

2
  • 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

2

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:

Controller

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

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

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

Markup

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

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

</apex:page>
0

You must log in to answer this question.

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