Tuesday, August 25, 2015

PASSING PARAMETERS from one vf to another vf



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>

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');}}


Using Param

how can we pass the id of the contact to the controller? The answer is straightforward, but requires a slight mindshift. 

The <apex:param> standard component allows parameters to be defined for a parent component, which can be assigned to a controller property. I can therefore change my markup to the following to specify the parameter:
?



<apex:commandButton value="Del" action="{!delCont}" rerender="all">  <apex:param name="contIdParam" value="{!cont.id}" assignTo="{!contIdChosen}"/>
</apex:commandButton>


Note the rerender attribute of the commandbutton - if this attribute is not present, the parameter does not get sent back to the controller. I simply have an outputpanel around the entire page contents.


I then add the following property to my controller:

1
public String contIdChosen {get; set;}


The key to this is the behaviour when the button is clicked - the id of the contact instance is assigned to my controller property before the action method is invoked. Thus the method that is actually carrying out the delete can simply access the contIdChosen property secure in the knowledge that it will contain the id of the contact associated with the button:

public PageReference delCont()
{
 Contact toDel=new Contact(id=contIdChosen);

 delete todel;

 setupContacts();

 return null;
}

  


The fact that I have to rerender in order to get the parameter passing to work is the reason why I maintain the list of Contacts independent of the Account record from the standard controller - I can't make the browser refresh the page and thus rebuild the related contacts from scratch, so I have to rebuild the list server side.


Through URL


You can simply pass the parameters through the URL.
say you are redirecting from one VF page to another
string value = 'your param value';
string url;
url = '/apex/VF_Page_Name?param1=' + value; 

PageReference pageRef = new PageReference(url);
pageRef.setRedirect(true);
return pageRef;
Then in the controller of the VF page, you just can get the param like this

String param_value = system.CurrentPageReference.GetParameters().get('param1');

No comments:

Post a Comment