Here is the problem. I have one controller for both pages and I need to have that controller because of view state. I am passing values from home to about us page. I am getting two different behaviors from returning page reference when I construct a page reference from url compared to when I return Page.Page Name. With aboutUs1, the view state is lost, but the url in the browser changes. from myapp/home to myapp/aboutus

With aboutUs2, the view state is preserved, but the url in the browser stays at myapp/home, even when the page is myapp/aboutus.

I need to preserve the view state, but also get the matching url in the browser. Am I doing something wrong, or is this how return Page.PageName works? Why is the view state lost in aboutUs1, when I am setting the redirect to false?!

public PageReference aboutUs1() {

    sfdcBaseURL  = URL.getSalesforceBaseUrl().toExternalForm();
    sfdcBaseURL = sfdcBaseURL + '/MyApp/AboutUs';
    PageReference pageRef = new PageReference(sfdcBaseURL);
    return pageRef;

    public PageReference aboutUs2() {
    return Page.AboutUs;
  • If the setRedirect is false the browser will not update the URL: salesforce.stackexchange.com/questions/9367/…
    – Eric
    Dec 10 '15 at 21:20
  • But in AboutUs2, I am not setting the redirect to false. The url is not updated in AboutUS2. AboutUs1 is updating the url even thought set redirect is set to false.
    – newcoder
    Dec 10 '15 at 22:17
  • About us 1 must be a different url then
    – Eric
    Dec 10 '15 at 22:19
  • aboutUs1 is updating the url in the browser, aboutUs2 is not updating the url in the browser.
    – newcoder
    Dec 10 '15 at 22:22
  • Like I said. The url returned by aboutus1 is not the same as the page you started at (minus page name) which is why the browser url updates and viewstate is lost
    – Eric
    Dec 10 '15 at 22:24

It seems that when you add the URL part to the page reference it is causing the redirect and thus the loss of view state.

Take the following examples and give them a try.

To see the difference comment and uncomment the appropriate lines:


public class pageController{

      public Account a {get;set;}

      public pageController(){
        a = new Account();

      public pagereference gotoPage2(){

        String sfdcBaseURL  = URL.getSalesforceBaseUrl().toExternalForm();

        //this line will cause a redirect and the viewstate to be lost
        //sfdcBaseURL = sfdcBaseURL + /apex/page2';

        //this ine will preserve the viewstate
        sfdcBaseURL = /apex/page2';

        PageReference pageRef = new PageReference(sfdcBaseURL);
        return pageRef;      



First Page

<apex:page controller="pageController" cache="false">

<apex:form >
<apex:inputField value="{!a.name}"/>
<apex:commandButton action="{!gotoPage2}" value="test" />


Second Page

<apex:page controller="pageController" cache="false">

You are on page 2



  1. When adding the externalForm URL to the page reference the redirect happens regardless of what you set the setRedirect to
  2. If you use the line without the externalForm URL the view state is maintained
  • Thanks Eric, this is doing the trick for maintaining the viewstate, but it's bringing me back to the problem I have with aboutUS2 method. It does not update the url in the browser.
    – newcoder
    Dec 11 '15 at 17:24
  • Right. Back to the original point. If you want to keep viewstate the url will not change. You cannot have both. You can however add parameters and use those to meet your needs. See link in my comment on your question
    – Eric
    Dec 11 '15 at 17:26
  • Thanks, this answers my question of whether or not I could have both.
    – newcoder
    Dec 11 '15 at 17:29

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.