PageReference Class

A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.

System

Use a PageReference object:

  • To view or set query string parameters and values for a page
  • To send the user to a different page as the result of an action method

In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of these ways.

  • Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists. Here, existingPageName is a placeholder value.

  • Creates a PageReference to any page that is hosted on the Lightning platform. For example, setting ‘partialURL’ to '/apex/HelloWorld' refers to the Visualforce page located at http://mySalesforceInstance/apex/HelloWorld. Here, mySalesforceInstance and partialURL are placeholder values. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record. Here, partialURL and recordID are placeholder values.

    This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Here, existingPageName is a placeholder value. Runtime references are not available to the referential integrity system. Consequently, the platform doesn’t recognize that this controller or controller extension is dependent on the existence of the specified page and won’t issue an error message to prevent user deletion of the page.

  • Creates a PageReference for fullURL, which is a placeholder value for an external URL. For example:

You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:

Here’s a non-exhaustive list of headers that are set on requests.

HeaderDescription
HostThe host name requested in the request URL. This header is always set on Lightning Platform Site requests and My Domain requests. This header is optional on other requests when HTTP/1.0 is used instead of HTTP/1.1.
RefererThe URL that is either included or linked to the current request’s URL. This header is optional.
User-AgentThe name, version, and extension support of the program that initiated this request, such as a web browser. This header is optional and can be overridden in most browsers to be a different value. Therefore, this header can’t be relied upon.
CipherSuiteIf this header exists and has a non-blank value, this means that the request is using HTTPS. Otherwise, the request is using HTTP. The contents of a non-blank value are not defined by this API, and can be changed without notice.
X-Salesforce-SIPThe source IP address of the request. This header is always set on HTTP and HTTPS requests that are initiated outside of Salesforce’s data centers. If a request passes through a content delivery network (CDN) or proxy server, the source IP address might be altered, and no longer the original client IP address.
X-Salesforce-Forwarded-ToThe fully qualified domain name of the Salesforce instance that is handling this request. This header is always set on HTTP and HTTPS requests that are initiated outside of Salesforce’s data centers.

This example shows how to use a PageReference object to retrieve a query string parameter in the current page URL. In this example, the getAccount method references the id query string parameter.

This page markup calls the getAccount method from that controller.

For this example to render properly, you must associate the Visualforce page with a valid account record in the URL. For example, if 001D000000IRt53 is the account ID, the resulting URL should be:

Replace Visualforce_URL with the Visualforce URL for your org. For production, this URL is in the format MyDomainName--PackageName.vf.force.com, where MyDomainName and PackageName are placeholder values. If your installed package is unmanaged, the package name is c. For more information on the format of the URLs that Salesforce serves for your org, see My Domain Login and Application URL Formats and Partitioned Domains in Salesforce Help.

The getAccount method uses an embedded SOQL query to return the account specified by the id parameter in the URL of the page. To access id, the getAccount method uses the ApexPages namespace.

  • First the currentPage method returns the PageReference instance for the current page. PageReference returns a reference to a Visualforce page, including its query string parameters.
  • Using the page reference, use the getParameters method to return a map of the specified query string parameter names and values.
  • Then a call to the get method specifying id returns the value of the id parameter itself.

Any action method in a custom controller or controller extension can return a PageReference object as the result of the method. If the redirect attribute on the PageReference is set to true, the user navigates to the URL specified by the PageReference.

This example shows how this can be implemented with a save method. In this example, the PageReference returned by the save method redirects the user to the detail page for the account record that was just saved.

This page markup calls the save method from that controller. When a user clicks Save, he or she is redirected to the detail page for the account just created:

This example shows how to redirect a user attempting to access a retired feedback site to a self-service help site. If the redirect attribute is set to true on the PageReference for the feedback site, the user navigates to the URL specified by the PageReference. The redirectCode attribute defines the redirection type for search engine optimization in public Experience Cloud sites.

This example shows how to call the RedirectController class from the retired site page.

To redirect a page that’s served by a third-party CDN, configure that CDN to pass the origin IP address via the true-client-ip HTTP header on the page. For more information, see Prerequisites for a Custom Domain That Uses a Third-Party Service or CDN in Salesforce Help.