Sunday, September 9, 2012

PageReference in Salesforce

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.
Use a PageReference object:
  • To view or set query string parameters and values for a page
  • To navigate the user to a different page as the result of an action method

Instantiation


In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of the following ways:
  • 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');
You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:
PageReference pageRef = ApexPages.currentPage();
 
 
PageReference Best Practice 
 


I have seen a lot of coders put the following into their custom Visualforce controllers:

public PageReference close() {

PageReference acctPage = new PageReference ('/' + acct.id};

acctPage.setRedirect(true);

return acctPage;


}


I've decided that I don't like this approach. It feels too much like a URL hack, and though I'm sure that it will always work

(meaning that salesforce.com will never change its way of referring to a record by/), I'd like to suggest a different

method that may use more resources, but will leverage standard controllers, possibly future-proofing the application:


public PageReference close() {


ApexPages.StandardController sc = new ApexPages.StandardController(acct);

PageReference acctPage = sc.view();

acctPage.setRedirect(true);

return acctPage;

}

You can easily get more information about different
PageReference methods in Apex language reference. Enjoy Coding.