Monday, March 7, 2011

Passing values between pages in Visualforce

I read a lot of information over the web scattered here and there. So I decided to blog about this. Often developers try to figure out what are the different ways through which values can be passed between pages in Visualforce. Well, there might be a number of them, but I would like to talk about the two most common ways to transfer data between the Visualforce web pages.


Method 1. Passing variable value between two pages that implement the same controllers and extensions.
Method 2. Passing values using a Querystring.


Method 1


In order to use the first method above, we need to ensure that the two pages in context need to have the same controller. This might be a Standard controller or a Custom controller.


Say we have to pages Page1 and Page2. Let the Visualforce syntax be as below:


Code for Page1
<apex:page controller="customcontroller1">
   <apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" />
</apex:page>


Code for Page2

<apex:page controller="customcontroller1">
   .The employee name is {!EmployeeName}
</apex:page>

Code for customcontroller1
public with sharing customcontroller1
{
  public string EmployeeName {get;set;} 
  
  // Function for navigating to next page
  public PageReference next()
  {
     PageReference nextpage = new PageReference('/apex/Page2');
     return nextpage;
  }
}

Now there are a few points worthy to note out here. You need to be careful of the method nextpage.setRedirect(true) , a method used to redirect from one page to another. Use it only when you use querystrings in the URL. If you use this method in the method above, it will wipe out all the data stored in variables in page1 being transferred to page2, as page2 in such a scenario will loose all the variable data of the controller, when this page2 loads.


Method 2

This method involves transferring variable data from one page to another, using a query string. This method should be used when the controller of the pages are different. See example below:

Say we have two pages, Page3 and Page4.

Code for Page3
<apex:page controller="customcontroller3">
   <apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" />
</apex:page>

Code for Page4
<apex:page controller="customcontroller4">
   .The employee name is {!EmployeeName}
</apex:page>

Code for customcontroller3
public with sharing customcontroller3
{
  public string EmployeeName {get;set;} 

  // Function for navigating to next page
  public PageReference next()
  {
     PageReference nextpage = new PageReference('/apex/Page4?EN=' + EmployeeName);
     return nextpage.setRedirect(true); //Note that setRedect method is used here
  }
}


Code for customcontroller4
public with sharing customcontroller4
{
  public string EmployeeName {get;set;} 
  
  //Constructor
  public customcontroller4()
  {
    String strEmpName = ApexPages.currentPage().getParameters().get('EN');
  }
 }

This way, you can transfer data from a page to another even if they don't have the same controller. 

There is another way to transfer data between pages, and that is using Javascript and hidden fields in the form. I will blog on that in my coming posts.

Till then, happy reading. Cheers!

Vishal

4 comments:

  1. Thanks Man!!!. The method 1 is really interesting and never thought of this before, the reason why we are able to get the data is because of the setRedirect method of the pagerefrence, great thought Man simple and effective.

    ReplyDelete
  2. Method 1 is not working for me.. I have a button, that calls a javascript, that builds a string and calls an actionfunction passing it as a parameter.. that method is supposed to return another page with the same controller, but the actual page just stays static

    ReplyDelete
  3. Method 2 is not setting the value and so it does not display. Add one line to fix:

    public customcontroller4()
    {
    String strEmpName = ApexPages.currentPage().getParameters().get('EN');
    this.EmployeeName = strEmpName;
    }

    ReplyDelete
  4. Suppose i have a list that contains about 500 records then..how wud i transfer that data from one controller to another..bec'z like if we are passing data using querystring then data will be truncated..as it will cross maximum length of url..

    ReplyDelete