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

    techtrekker Correct answer

    9 years ago

    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.


    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.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used