What is a Parameter?
A parameter is a name value pair that goes into the URL of a page. The first parameter starts with a '?' and the subsequent start with a '&'.
http://www.google.com?browser=chrome&version=12
We have two parameters in this URL. browser with a value of 'chrome', and version with a value of '12'.
Many a times you would want to pass parameters to a visualforce page..
There are a number of Situations in which you would want this. This article covers a few of them.
Scenario 1 - From a Custom Button.
When you create a custom button on "Account", you will only have the option of selecting Visualforce pages that have the "standardcontroller="Account"" attribute.To overcome this restriction, select the content source as URL and not visualforce... Then you could type the URL as \apex\yourpage.. And what more you can now pass parameters as you need using merge fields... This method has got one more advantage.. You are not forced to use a standard controller with an extension just for your page to appear in the list when you actually do not need a standard controller..
Situation 2 - From a Link.
Here is a small sample program
Step 1: Create a Visualforce page called SamplePage1....
<apex:page >
<apex:outputlink value="/apex/SamplePage2"> Click Here <apex:param name="msg" value="success"/> </apex:outputlink>
</apex:page>
Step2: Create a Visualforce page called SamplePage2
<apex:page controller="Sampleclass"> </apex:page>
Step 3: On SamplePage1 Click on the " Click Here" link. You will see that when you click on the link you will navigate to the SamplePage2. Check the URL now..
https://na5.salesforce.com/apex/SamplePage2?msg=success
You can see that the parameter " msg" with value "success" has been passed to the new page.
Pagereference().getParameters().put()
PageReference is a type in Apex. Read the documentation here.
When you click a command button or a command link in a visualforce page, you call a method. The return type
of this method would be a PageReference meaning that, after clicking the button the user will be redirected to a
new Page. Below is a sample code, which shows how to pass parameters from a PageReference
public Pagereference gotonewpage()
{
PageReference pageRef = Page.existingPageName;
pageRef.getParameters().put('msg','success');
return PageRef
}
existingPageName - actual name of a visualforce page
Retrieve Parameter Values in Apex Class:
The Apex Class code would be
public class Sampleclass
{
Public String message = System.currentPagereference().getParameters().get('msg');
}
So now, the variable "message" would store the value of your parameter "msg"....
How to Cover this code in TEST Method?????
public class Sampleclass
{
Public String message = System.currentPagereference().getParameters().get('msg');
public static testmethod void SampleclassTest()
{
System.currentPagereference().getParameters().put('msg','success');
}
}
And at last some suggestions...
Be careful with the values you pass in the URL.. Special Characters will cause problems
when passed in the URL.. For ex if you pass the Account Name "Shah & shah".. this would'nt
pass properly as the special character "&" can't get into the URL..
So as a best practice always pass ID's.. you could write a query and retrieve any other field
in the APEX Class