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.
All about Salesforce Release Spring 21 here.
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 –
- 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
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 –
- 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. 🙁
- 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.