function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ericmonteericmonte 

Visualforce Save Command Button Redirect

I'm drawing a blank on how to use the standard {!Save} function in my visualforce on the command button. Basically after I click Save I want to redirect the page into a new page.

Here is my VF page code

<apex:page standardController="Lead" recordSetVar="leads" id="thePage" showHeader="true" sidebar="false" extensions="extContactApprovalPage" >
 
  <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!leads}" var="l">
                <apex:column >
                    <apex:outputField value="{!l.Name}"/>
                    <apex:facet name="header">Registrant Name</apex:facet>
                </apex:column>
               
                <apex:column >
                    <apex:outputField value="{!l.Approve_Picklist__c}"/>
                    <apex:facet name="header">Approve Picklist</apex:facet>
                </apex:column>
                <apex:column >
                    <apex:outputField value="{!l.Approve_Checkbox__c}"/>
                    <apex:facet name="header">Approve Checkbox</apex:facet>
                </apex:column>
               
              <apex:inlineEditSupport event="onClick" showOnEdit="saveButton, cancelButton"/>
             
          </apex:pageBlockTable>
          <apex:pageBlockButtons >
                <!--<apex:commandButton value="Edit" action="{!save}" id="editButton" />-->
                <apex:commandButton value="Save" action="{!save}" id="saveButton" reRender="none" />
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" />
            </apex:pageBlockButtons>
      </apex:pageBlock>
 
  </apex:form>
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page: ContactApprovalPage
  <!-- End Default Content REMOVE THIS -->
</apex:page>

and here is my extension:

public class extContactApprovalPage {

    public extContactApprovalPage(ApexPages.StandardSetController controller) {
      
    }
   
    public PageReference save(){
       
        
        PageReference reRend = new PageReference('/003');
        return reRend;
    }

}

Any assistance would be great thanks.
Best Answer chosen by ericmonte
Phillip SouthernPhillip Southern
Hi Eric, that's because you've overridden the Save command and it running it from your extension....where the only code is to perform the page redirect.  You'll need to write a save dml statement there before the redirect.

All Answers

Phillip SouthernPhillip Southern
Hey Eric...for your save button, take out the rerender attribute.  You don't want to do a rerender of a certain section of the page, you want to return a full page reference.

So that line should read:
<apex:commandButton value="Save" action="{!save}" id="saveButton" />

Another thing you can try if that doesnt work, is set redirect to true on the pagereference...so that would look like this:

PageReference reRend = new PageReference('/003');
reRend.setRedirect(true);
        return reRend;

ericmonteericmonte
Phillip

The redirect works, but unfortunaltely my Save Functiontionality didnt work. For example in my sample i had an inline edit where users can click the checkbox. Once they check the box and click save, it should save thee record and redirect them to a Thank You page.

I was able to get the redirec to work but the Records wasn't saved. Any ideas?
Phillip SouthernPhillip Southern
Hi Eric, that's because you've overridden the Save command and it running it from your extension....where the only code is to perform the page redirect.  You'll need to write a save dml statement there before the redirect.
This was selected as the best answer
ericmonteericmonte
OMG! Thanks Phillip, I can't believe I missed that part.
Phillip SouthernPhillip Southern
haha no problem! :)  
ericmonteericmonte
So in the end this is how my apex class looks like and very simple! Thanks for the help phil!

public class extContactApprovalPage2 {
    ApexPages.StandardController stdCtrl;
    Public List <Lead> lead {get;set;}

    public extContactApprovalPage2(ApexPages.StandardController controller) {
        stdCtrl= controller;
        lead = [select id, Name, Approve_Checkbox__c, Approve_Picklist__c, Email__c, Email from Lead where Email__c != null and isConverted= false];

    }
   
     public PageReference save(){
       
        upsert lead;
      
        PageReference reRend = new PageReference('/apex/ThankYou');
        reRend.setRedirect(true);
        return reRend;
    }

}
joestuartjoestuart
Hi Ericmonte,

I need to do th esame thing, I want to save and then redircet to a different page.  Thank you for posting your code.  My problem is with the unit testing.  Would you be kind enough to post your test code too?

Thank you.
Jeff BergerJeff Berger
Hi All,
Stumbled across this as I was looking to do the same thing--is there any way to do it without writing a custom save action? Ideally of course I'd love to do this without needing to write an extension.
Thanks!
Jeff
Sakthi RSakthi R
Hi,
I am using custom controller. i need to redirect standard salesforce new account creation page to my vf page when standard save is clicked.
any ideas would help me alot..??
ericmonteericmonte
Sakthi, I'm confused on what you are doing.
Only Standard Controller uses the standard save action method, and if you want to override that then you use an extension on top of that.

If you are using a custom Controller, then you would have to define all your methods and save functionality. So if I was doing a SaveMethod() for an Account and redirect it to the newly created Account Page I would do something like:


Public PageReference SaveMethod(){
Acccount acc = new Account(
Name = 'Acc Test');

insert acc;


PageReference pageRef = new PageReference('/'+acc.id);

return pageRef;

}

Hope this helps! And please look at the documentation below if you have any other questions on this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_setter_methods.htm
paul rodgers 3paul rodgers 3

I have a single controller extension I use for all VisualForce pages that use a standard controller

In my case I want to pass the "retURL" parameter and have a save use that rather than redirect to the newly created detail page.
The nice bit here is that the same controller extension works for all object types.
I just replace the {!save} action in the page with {!submit}

// controller extension used by any VF page that wants to do a save-and-redirect
// so that the user does NOT end up on the detail page after saving a record
// it will call the standard save method, then redirect to the retURL parameter 
public class submit_retURL_extn {
        
// Constructor 
private ApexPages.StandardController sc;
public submit_retURL_extn(ApexPages.StandardController stdController) {
        this.sc = stdController;
}
  
public PageReference submit() {
    // fetch the redirect from the url
    String next_url = ApexPages.currentPage().getParameters().get('retURL');
    
    sc.save();
    
    if ( next_url != null && next_url.length() > 0 ){
        PageReference next_page = new PageReference(next_url);
        next_page.setRedirect(true);
        return next_page;
    }else{
        return(null);
    }
}
}

Mike ArthurMike Arthur
Hi Paul,

The submit_retURL_extn looks pretty close to what I need.

I call a VF page with standard controller and this extension from my Quote page.  The VF page shows just one field - a custom field called 'Site' which is a lookup to Account.

On the VF page the user can update this field to point to a different account.

When 'Submit' is pressed, the 'Site' custom field value is updated and the new account name and Id in 'Site' must be passed to the next VF page.

How can I pick up the updated value of name and Id for 'Site' to pass to the next page?

Thanks,
Mike.
ericmonteericmonte
There are few ways you can do it.
One way is the to pass the id or the name as part of your url parameters when you do a redirect on a page reference.

hopefully that helps for now
paul rodgers 3paul rodgers 3
Mike, you'll have to make an extension thats specific to your page.
Then when composing the url your going to send the user to just stick the ad on the end, so it'll be someting likeL

ret_URL = ret_URL + '&id=' + {!Site}

The new page will get an "id" parameter with the account id in it from the page with the custom lookup field.
Remember, if its the first parameter in the URL its a "?" not an "&"
 
Mike ArthurMike Arthur
Thanks guys, feels like I'm really close with this, but the old site is still getting passed, how do I ensure the new site is passed?

Here's Paul's code that I have hacked to my needs:
 
public class submit_retURL_extn {
        
// Constructor 
//private ApexPages.StandardController sc;
//public submit_retURL_extn(ApexPages.StandardController stdController) {
//        this.sc = (Quote)stdController.getRecord;
    private final Quote qte;
	public submit_retURL_extn(ApexPages.StandardController stdController) {
    this.qte = (Quote)stdController.getRecord();
}
  
public PageReference submit() {
    // fetch the redirect from the url
//    String next_url = ApexPages.currentPage().getParameters().get('retURL');
    
//    sc.save();
    update qte;
    
system.debug('Site Name: '+qte.QW_Site__c+'; Site Id: '+qte.QW_Site_ID__c);
    String next_url = '/apex/ManageQWList?parentId='+qte.Id+'&QWSiteId='+qte.QW_Site_Id__c+'&QWSiteName='+qte.QW_Site__c;
system.debug('next_url: '+next_url);
    if ( next_url != null && next_url.length() > 0 ){
        PageReference next_page = new PageReference(next_url);
        next_page.setRedirect(true);
        return next_page;
    }else{
        return(null);
    }
}
}



Many Thanks.
ericmonteericmonte
Since you are doing a partial url with SFDC you have to follow to update your line 23
PageReference next_page = new PageReference('/'+next_url);

And that should work.
Documentation is here
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm
ericmonteericmonte
Mike,

Sorry try changing line 23 to the following: 
PageReference next_page = new PageReference(''+next_url);

Sometimes the text string acts funky... also make sure to check if your code actually gets in that if statement.
Mike ArthurMike Arthur
Hi Eric,

Thanks for that - the url is actually working fine, the second VF page displays and the parameters are passed.

My problem is that it is the original account in the lookup field on the first page that is being passed to the second page, not the updated account.  (The user sees a lookup to account on the first page, updates it and goes to the second page.  The value of the account lookup is used for the records entered on the second page).

I'm missing something to do with refreshing the values in the fields.  With Javascript I managed to get the updated account name to pass across but the old Id was still being sent.  I then opted to try the controller extension route.

Any ideas?

Many Thanks,
Mike.
Mike ArthurMike Arthur
Ok, it's working :-)

Problem was that using the JS route, the name of the account was being passed so I created a custom formula field to get the id.  It was the value of the formula field that I was trying to use to pass the Id and that doesn't appear to get updated.

With the controller extension route, the id is passed as the value for the Account field.  Using that instead of the formula field, the updated value gets passed, as everyone above says would be the case!

Thanks to all and remember not to use formula fields to pass values!