How does setRedirect work in Visualforce

What is setRedirect

SetRedirect() is a method of PageReference class. When a PageReference value is used as a return value for a commandButton or actionFunction method, the setRedirect() value defines whether it is a client-side redirect or a server-side forward. According to Salesforce document here:

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

Note that if the URL of the PageReference object is set to a website outside of the salesforce.com domain, or to a page with a different controller or controller extension, the redirect always occurs, regardless of whether the redirect attribute is set to true or false.

This explanation is definitely very accurate. However, due to it is a bit too technical, it might be a little bit hard for new developers to understand.

For other Visualforce developers, we can simply understand this as:

  1. If setRedirect is false, the controller will remember the values stored in View State. So all the variable values will be kept. And the constructor won’t be run again.
  2. If setRedirect is true, the controller will forget everything about View State. And the constructor will be run once again.
  3. If two pages are of difference controllers, the setRedirect value will always be true regardless of how you set it.

Default Value of setRedirect

According to Salesforce documentation, the default value for setRedirect is true. In most cases, it is. But there is there is one exception – if the original page and redirect page is the same one, the default setRedirect value is false. Consider the following code:

Controller:


public with sharing class TestRedirectController 
{
    public String inputValue {get; set;}

    public TestRedirectController() 
    {
        inputValue = 'Hello';
    }

    public PageReference reloadPage() 
    {
    	//return null;
    	//return Page.TestRedirect;

    	pageReference pr = Page.TestRedirect;
        pr.getParameters().put('id', '11123434');
        return pr;
    }
}

Page:


<apex:page controller="TestRedirectController">
    <apex:form>
        <apex:inputText value="{!inputValue}"/>
        <apex:commandButton value="Reload Page" action="{!reloadPage}"/>
    </apex:form>
</apex:page>

This is a very simple page. Load the page, change the value in the input to be ‘World’ or anything you like. Then click the reload page button. Please note that after reloading, the input is not changed back into ‘Hello’.

Feel free to change the controller code to be return null; or return Page.TestRedirect. The result would be the same. This code simply shows that even if we changed the query strings of the url, the page setRedirect() is still false.

Now we can update the code and add pr.setRedirect(true) into the reloadPage() code. Note that, the input box will always be ‘Hello’ in this scenario.

Other scenarios

In other scenarios, the default value will be true. Please note, if you want to keep the View State values, the controller of original page and redirect page has to be exactly the same, which means both controller and the extensions class.

Next Post

How to refresh page from an embedded Visualforce page

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *