Passing parameters to a visualforce page and between pages
9:31 AMWhat 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
25 comments
just what i needed ! thanks
ReplyDeleteNice workaround. I've got an action="{!autoRun}" prop in my VF Page as in:
ReplyDeleteapex:page standardController="Task"
extensions="VFController"
action="{!autoRun}">
You tried calling Apex Code from a button. If you see this page, something went wrong. You should have
been redirected back to the record you clicked the button from.
Special thanks to Scott Hemmeter for this example. Anyway, I have a button that fires off the event. As long as the button has its Content Source set to Visualforce Page, !auto run triggers. However, if I set Content Source to URL and then point to \apex\VFTaskMachine (which is my VF Page), the browser comes back saying that the URL doesn't exist?! I can't even hit this page manually by entering the full URL in web browser. It's really weird as I can load up my other test apex pages just fine.
Any ideas??
Thanks dude.I am good to start with it.... :) Keep posting.
ReplyDeleteThanks buddy.. :) will try and post more
ReplyDelete🚀 Super informative post on passing parameters to and between Visualforce pages! The examples are crystal clear 💎 and cover all the key scenarios like custom buttons 🔘, links between pages 🔗, and using PageReference. The test class example 🧪 is really helpful too. Overall, a fantastic resource for anyone working with Visualforce and parameter passing in Salesforce! 👏🏼👍🌟
DeleteVery Clear Explanation..
ReplyDeleteNice post. Thanku so much!
Thankyou!.....Your Article was of immense help in a project I'm trying to do.
ReplyDeletehow to pass a list from one page to another page
ReplyDeleteThanks Dude.. but i am still with doubts to get clarified..
ReplyDeleteThankyou....I have passed the parameters using id.....But still not able to retrieve the other fields on the next page. Can you please give me a sample code for this.
ReplyDeleteIf you have a Custom Button on a Salesforce "std" page that directs to a VF page that presents a list of records to a user from which they choose one of them, how would you get the selected record ID back into the "std" SF page to populate into a field on the page?
ReplyDeleteCan that be done?
is there any way to hide this param from the URL ?
ReplyDeleteNot sure if you could do it.. You might try using the URLENCODE function .. Not sure if Pagereference and SetRedict as false would work but you can try
ReplyDeletehow to pass record values between two pages which have one controller 2 pages, records may be more than one
ReplyDeletewhen you have the same controller, then you need not pass values... When you move between the pages set redirect = false in your pagerefernce methods.. This way the methods and variables retain the same value even when you move from one page to another.. You may want to read about "viewstate" if what i said is confusing...
DeleteI am trying to pass custom field of ‘currency’ type from Account object to another currency field in a Custom object ‘XYZ’ (related list of Account) through a new custom button. In the URL, the currency value is being passed along with the currecy code.. i.e., SGD 9,000,000. The same is populated in the custom field, so it gives me an error – ‘Invalid currency’ as the currency field only accepts numbers not the text(SGD). If I take out SGD manually after it is populated and save the record, I get no error.
ReplyDeleteI need to make sure SGD is not passed in the URL. I tried SUBSTITUTE and others,but nothing worked. Any ideas?
thanks
ReplyDeleteAwesome Explanation!! I am very new to programming but i can still understand the contnet. Can you please try to post something on Wrapper Classes in a similar way?
ReplyDeleteThank You.
How to pass parameter as hidden to VF page
ReplyDeleteAwesome..but i have exact same use case
ReplyDeleteif you pass the Account Name "Shah & shah".. this would'nt
pass properly as the special character "&" can't get into the URL..
how do i pass value like "Shah & shah"... Appreciate your help..
Good post but i want to know how to pass list variable from visualforce page to controller class
ReplyDeleteHi Can you tell how to pass a query sting parameter to custom VF report on dashboard?
ReplyDeleteThanks
thanks very helpfull
ReplyDeleteI would like to call the parameter of pagereference method to another class. How can I achive this functionalty?
ReplyDeleteDummy Code-
public pageReference testmethod(){
CallingDetails(test, test1);
pageReference page;
pageReference pageRef = new PageReference('/apex/test');
pageRef.getParameters().put('test',test);
pageRef.getParameters().put('test1',test1);
if(test == testMap.get(RegId) && test1 == testMap.get(RegId)){
if(test != null && test1 != null){
page = new Pagereference('/apex/test1');
page.setRedirect(true);
}
}
return page;
}
public void CallingDetails(String test, String test1){
System.debug('test--'+test);
System.debug('test1--'+test1);
Studentlist = new list();
for(test t : [Select Id, test, test1 from test where test =:test AND test1=:test1]){
}
}
Hello i have three dropdown and each dropdown have sub menu. How to send in url dropdown value + sub menu value.
ReplyDelete