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}" />

Edit : You can proxy the invocation of the Standard Save method via an ActionFunction, but then override the redirect by using the onComplete to redirect to a custom URL. Here is a sample I did for overriding the New button for Contact : (By default the Save method returns you to the Contact newly created, however using this I managed to get it to return to the Account I clicked the New Contact button on)

<apex:page standardController="Contact">

var returnURL;

window.onload =  function(){
returnURL = gup('retURL');
//alert('Set retURL = ' + returnURL);


function gup( name ){  //this function just grabs HTTP params by name

name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
var regexS = "[\\?&]"+name+"=([^&#]*)"; 
var regex = new RegExp( regexS ); 
var results = regex.exec( window.location.href );
 if( results == null )    return ""; 
else    return results[1];}

function redirectBack(){
//alert('Sending you back to ' + returnURL);
window.location.href = '/' + returnURL;


<apex:form >
<apex:actionFunction  name="saveActionFunc" action="{!Save}" oncomplete="redirectBack()" rerender="theBlock" />

<apex:pageBlock mode="Edit" id = "theBlock" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" onClick="saveActionFunc();"/>

<apex:pageBlockSection >
<apex:inputField value="{!Contact.LastName}" />
<apex:inputField value="{!Contact.FirstName}" />  
<apex:inputField value="{!Contact.Email}" />   


In summary, it does seem possible to do without a Controller! (Although I noticed some javascript race issues at times, but works for the most part)

My understanding of retURL is the location you want to redirect to when the user hits Cancel. To control the page to navigate to on Save, the saveURL is used.

When creating a new record from inspecting the URL, it also adds save_new_url=/003/e for Contact. I would try adding this.

Could you post the URL you have set the new button to redirect the user to when they click new. (what I usually do is copy the URL that salesforce by default sends you to when clicking new)

I would also try adding noOverride=1 to the URL.

  • apex/CostNew?CF00NU0000001LfsZ=CA+002491484&CF00NU0000001LfsZ_lkid=a08U0000006tigw&scontrolCaching=1&retURL=%2Fapex%2Fipfoliodev__IPRightDetails%3Fid%3Da08U0000006tig
    – doga
    Oct 31 '12 at 9:02
  • I'm working on a way to proxy the save through an actionFunction so that I can invoke a redirect to a custom page via the onComplete. Should hopefully post my observations shortly. Oct 31 '12 at 19:19

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();
        return cancel;


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


<apex:commandButton action="{!saveAndReturn}" value="Save And Return"/>
  • 1
    For some reason controller.cancel() returns me to the standard record, not the VF Page. In order to get the VF Page (with GET parameters etc) I had to do PageReference cancel = new PageReference(ApexPages.currentPage().getHeaders().get('Referer')); Jun 30 '13 at 17:51

I think this is the closest to what Salesforce does on their standard pages, but it does require a controller. With this method the visualforce page will redirect to the returl parameter if it's there, otherwise it'll go to the record you just created.

public pageReference save(){    
    PageReference pageRef = con.save();
Map<string,string> URLParameters = ApexPages.currentPage().getParameters();
    pageRef = new PageReference(URLParameters.get('retURL'));
return pageRef;

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.