Passing parameters to a visualforce page and between pages

9:31 AM


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.

When you want to pass parameters between visualforce pages use the <apex:param> tag....

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>

Step2Create 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


24 comments

  1. just what i needed ! thanks

    ReplyDelete
  2. Nice workaround. I've got an action="{!autoRun}" prop in my VF Page as in:

    apex: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??

    ReplyDelete
  3. Thanks dude.I am good to start with it.... :) Keep posting.

    ReplyDelete
  4. Thanks buddy.. :) will try and post more

    ReplyDelete
  5. Very Clear Explanation..
    Nice post. Thanku so much!

    ReplyDelete
  6. Thankyou!.....Your Article was of immense help in a project I'm trying to do.

    ReplyDelete
  7. how to pass a list from one page to another page

    ReplyDelete
  8. Thanks Dude.. but i am still with doubts to get clarified..

    ReplyDelete
  9. Thankyou....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.

    ReplyDelete
  10. If 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?

    Can that be done?

    ReplyDelete
  11. is there any way to hide this param from the URL ?

    ReplyDelete
  12. Not 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

    ReplyDelete
  13. how to pass record values between two pages which have one controller 2 pages, records may be more than one

    ReplyDelete
    Replies
    1. when 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...

      Delete
  14. I 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.

    I need to make sure SGD is not passed in the URL. I tried SUBSTITUTE and others,but nothing worked. Any ideas?

    ReplyDelete
  15. Awesome 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?

    Thank You.

    ReplyDelete
  16. How to pass parameter as hidden to VF page

    ReplyDelete
  17. Awesome..but i have exact same use case
    if 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..

    ReplyDelete
  18. Good post but i want to know how to pass list variable from visualforce page to controller class

    ReplyDelete
  19. Hi Can you tell how to pass a query sting parameter to custom VF report on dashboard?

    Thanks

    ReplyDelete
  20. thanks very helpfull

    ReplyDelete
  21. I would like to call the parameter of pagereference method to another class. How can I achive this functionalty?

    Dummy 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]){
    }
    }

    ReplyDelete
  22. Hello i have three dropdown and each dropdown have sub menu. How to send in url dropdown value + sub menu value.

    ReplyDelete