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.
Page.existingPageName
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.
PageReference pageRef = new PageReference('partialURL');
Creates a PageReference to any page that is hosted on the Force.com platform. For example, setting 'partialURL' to '/apex/HelloWorld' refers to the Visualforce page located at http://mySalesforceInstance/apex/HelloWorld. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record.
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. 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.
PageReference pageRef = new PageReference('fullURL');
Creates a PageReference for an external URL. For example:
PageReference pageRef = new PageReference('http://www.google.com');
PageReference pageRef = ApexPages.currentPage();
PageReference methods are all called by and operate on a particular instance of a PageReference.
The table below describes the instance methods for PageReference.
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| getAnchor | String | Returns the name of the anchor referenced in the page’s URL. That is, the part of the URL after the hashtag (#). | |
| getContent | Blob | Returns the output of the page, as displayed to a user in a
Web browser. The content of the returned Blob is dependent on how
the page is rendered. If the page is rendered as a PDF, it returns
the PDF. If the page is not rendered as a PDF, it returns the HTML.
To access the content of the returned HTML as a string, use the toString Blob method. This method
can't be used in:
If there's an error on the Visualforce page, an ExecutionException is thrown. |
|
| getContentAsPDF | Blob | Returns the page as a PDF, regardless of the <apex:page> component's renderAs attribute. This method
can't be used in:
|
|
| getCookies | Map<String, System.Cookie[]> | Returns a map of cookie names and cookie objects, where the key is a String of the cookie name and the value contains the list of cookie objects with that name. Used in conjunction with the Cookie class. Only returns cookies with the “apex__” prefix set by the setCookies method. | |
| getHeaders | Map<String, String> | Returns a map of the request headers, where the key string
contains the name of the header, and the value string contains the
value of the header. This map can be modified and remains in scope
for the PageReference object. For instance, you could do:PageReference.getHeaders().put('Date', '9/9/99'); For a description of request headers, see Request Headers. |
|
| getParameters | Map<String, String> | Returns a map of the query string parameters that are included
in the page URL. The key string contains the name of the parameter,
while the value string contains the value of the parameter. This map
can be modified and remains in scope for the PageReference object.
For instance, you could do:PageReference.getParameters().put('id', myID);Parameter keys are case-insensitive. For example: System.assert(
ApexPages.currentPage().getParameters().get('myParamName') ==
ApexPages.currentPage().getParameters().get('myparamname')); |
|
| getRedirect | Boolean | Returns the current value of the PageReference object's redirect attribute. Note that if the URL of the PageReference object is set to a website outside of the salesforce.com domain, the redirect always occurs, regardless of whether the redirect attribute is set to true or false. |
|
| getUrl | String | Returns the relative URL associated with the PageReference when it was originally defined, including any query string parameters and anchors. | |
| setAnchor | String Anchor | System.PageReference | Sets the URL’s anchor reference to the specified string. For example, https://Salesforce_instance/apex/my_page#anchor1. |
| setCookies | Cookie[] cookies | Void | Creates a list of cookie objects. Used in conjunction with the Cookie class. |
| setRedirect | Boolean redirect | System.PageReference | Sets the value of the PageReference object's redirect attribute. If set to true, a redirect is performed through
a client side redirect. 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. |
The following table is a non-exhaustive list of headers that are set on requests.
| Header | Description |
|---|---|
| Host | The host name requested in the request URL. This header is always set on Force.com Site requests and My Domain requests. This header is optional on other requests when HTTP/1.0 is used instead of HTTP/1.1. |
| Referer | The URL that is either included or linked to the current request's URL. This header is optional. |
| User-Agent | The 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 should not be relied upon. |
| CipherSuite | If 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-SIP | The 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. |
| X-Salesforce-Forwarded-To | The 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. |
The following 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:
public class MyController { public Account getAccount() { return [SELECT Id, Name FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('Id')]; } }
The following page markup calls the getAccount method from the controller above:
<apex:page controller="MyController"> <apex:pageBlock title="Retrieving Query String Parameters"> You are viewing the {!account.name} account. </apex:pageBlock> </apex:page>
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:
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.
The following 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:
public class mySecondController { Account account; public Account getAccount() { if(account == null) account = new Account(); return account; } public PageReference save() { // Add the account to the database.
insert account; // Send the user to the detail page for the new account. PageReference acctPage = new ApexPages.StandardController(account).view(); acctPage.setRedirect(true); return acctPage; } }
The following page markup calls the save method from the controller above. When a user clicks Save, he or she is redirected to the detail page for the account just created:
<apex:page controller="mySecondController" tabStyle="Account"> <apex:sectionHeader title="New Account Edit Page" /> <apex:form> <apex:pageBlock title="Create a New Account"> <apex:pageBlockButtons location="bottom"> <apex:commandButton action="{!save}" value="Save"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Account Information"> <apex:inputField id="accountName" value="{!account.name}"/> <apex:inputField id="accountSite" value="{!account.site}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>