Find your content:

Search form

You are here

Redirecting back to original page using visualforce


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

Attribution to: doga

Possible Suggestion/Solution #1

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.

Attribution to: techtrekker

Possible Suggestion/Solution #2

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

Attribution to: Andrew Fawcett

Possible Suggestion/Solution #3

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 =;
Map<string,string> URLParameters = ApexPages.currentPage().getParameters();
    pageRef = new PageReference(URLParameters.get('retURL'));
return pageRef;

Attribution to: user3405
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content