FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

I've overridden the standard button "New" of a custom object with a VF page. On this page, for saving record I'm using standard "save" action. But on saving, it's not redirecting back to the original page. I've checked retURL and it is passing correctly. I know I can create extension class and override save action. But can we do without that?

<apex:commandButton value="Save" action="{!save}" />

{ asked by doga }

ANSWER

According to the documentation for the StandardController save method...

Saves changes and returns the updated PageReference.

So does not actually seem respond to the retURL, despite it being present in the URL in this case. Bit of a tease! What it does is go back to the View page of your custom object.

What you ideally want to do is chain the 'save' and 'cancel' actions on this controller. I've had a try to do this without code in the VF page (tried actionFunction).

In the end, as you say, an extension controller was the only solution I found...

public with sharing class SaveAndReturnController
{
    private ApexPages.StandardController controller;

    public SaveAndReturnController(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }

    public PageReference saveAndReturn()
    {
        PageReference cancel = controller.cancel();
        controller.save();
        return cancel;
    }
}

Then...

<apex:page standardController="Test__c" extensions="SaveAndReturnController" tabStyle="Test__c">

And...

<apex:commandButton action="{!saveAndReturn}" value="Save And Return"/>

{ answered by Andrew Fawcett }
Tweet