Friday, September 17, 2010

Passing parameters to a visualforce page and between pages

Many a times you would want to pass parameters to a visualforce page..

Well, there are two situations in which you would want this.. One is when you want to pass parameters on the click of a custom button.. Other is when you want to pass parameters from one visualforce page to another....

Situation 1
You will see that when you create a custom button and select the source as visualforce page you would see only the page selection and no other option... Well there is a work around for this..

Select the content source as URL and not visualforce... Then you can type the URL as \apex\yourpage.. And what more you can now pass as many parameters as you need using merge fields... There is one more advantage in this method.. 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
When you want to pass parameters between visualforce pages use the <apex:param> tag....

Here is a small sample program

Step 1: First we 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>



Step 2: Save this page and now click on " Click Here" link. You will see that when you click on the link you will navigate to a new page called SamplePage2. Check the URL now..


https://na5.salesforce.com/apex/SamplePage2?msg=success&msg2=Story...

You can see that the parameter " msg" with value "success" has been passed to the new page.


Now, how will you retrieve this value in your APEX CLASS for processing.... see below....


Step3: We will put the following code in our SamplePage2

<apex:page controller="Sampleclass"> </apex:page>


The Apex Class code would be


public class Sampleclass
{
Public String message = System.currentPagereference().getParameters().get('msg'); 
Public String message = System.currentPagereference().getParameters().get('msg2');  
}



So now, the variable "message" would store the value of your parameter "msg"....


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 "&" cant get into the URL..



So as a best practice always pass ID's.. you can write a query and retrieve any other field
in the APEX Class


2 comments:

  1. Hi
    When i click the edit button of the contact record it shld take me to vf page and display all the data of that record in the vf page along with record ID... Please help me out on this

    ReplyDelete
  2. When i click the edit button of the contact record it shld take me to vf page and display all the data of that record in the vf page along with record ID... Please help me out on this.

    ReplyDelete