Sunday, August 2, 2015

HOW TO PASS PARAMETERS TO VISUALFORCE PAGES AND BETWEEN PAGES


I am sure we all came to different situations where we need to pass parameters to VisualForce pages while doing Salesforce development. So I thought of putting all scenarios together in a single post with the solution approach. So without any further introduction, let's get into the actual agenda quickly.

First let me explain the basic -

WHAT IS A PARAMETER ?
A parameter is a name-value pair which is embedded at the end of the url of a page. The first parameter always starts with '?' and all the subsequent parameters start with a '&'.
Let's take the below example -

http://sudipta-deb.blogspot.in/?firstname=Sudipta&lastname=Deb
  • The first parameter is firstname=Sudipta where name is firstname and value is Sudipta
  • The second parameter is lastname=Deb where name is lastname and value is Deb
With the basic explained, let me start with different scenarios and solution approach

SCENARIO 1 - PASSING PARAMETER FROM CUSTOM BUTTON
Sometimes it may be required that by clicking on custom button we need to open a new VisualForce page and pass parameters to that new page. For example - if we create a custom button for Account object, while configuring if we choose VisualForce page we can select only those VisualForce pages having standardController="Account" set. To overcome this, we need to choose Content Source as url while configuring Custom Button. Screenshot is given below -
Below is the VisualForce Page - showAccountName

SCENARIO 2 -PASSING PARAMETERS THROUGH COMMAND LINK OR COMMAND BUTTON
In order to pass parameters from one VisualForce page to another one, we can use command link or command button. Let's start with an example where clicking on command link a new VisualForce Page will be displayed along with the message passed from the previous page.

First create a simple VisualForce Page - FirstPage

As you can see in this page, I have used a command link which is passing a parameter with key value pair as firstName=Sudipta to a new Page - SecondPage

So now create the second VisualForce Page - SecondPage


As you can see that SecondPage needs a controller - SecondPageController. This controller will capture the parameter passed to it and set it to a variable.
Here comes the controller - SecondPageController


In this controller, the below line is important which is used to capture the message passed from FirstPage -
firstName = System.currentPageReference().getParameters().get('firstName');

With this approach, we need to keep in mind few things -

  1. All the parameters we are passing this way will be displayed in the page url. So if you are thinking of passing some confidential information, better not to do that. Otherwise you confidential stuffs will become public properties. :-(
  2. If you want to pass something having special character like accountName as "Batman & Superman", it will create problem. So it is always better to pass Salesforce ID or something unique identification field.
Please pass your comments. Thanks.

5 comments:

  1. Should add the scenario in which both pages have the same controller and navigation is done by actions returning a PageReference. Data is maintained in the controller as long as the PageReferences don't have redirect= true.

    ReplyDelete
    Replies
    1. Hi Joseph, I have written another post with the details you mentioned. Here is the link - http://sudipta-deb.blogspot.in/2015/08/how-to-pass-parameters-between-two.html. Please have a look and provide your feedback. Thanks.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. The blog gave me idea to pass parameter to visual force My sincere thanks for sharing this post and please continue to share this kind of post
    Salesforce Training in Chennai

    ReplyDelete