Solving problem with apex:param attribute working only with static fields
As you probably are well aware, <apex:param> is a very useful and widely used tool. In a nutshell, you can use it to pass values from the Visualforce Page to the Apex Controller. It is quite simple, yet it may pose some problems.
As a quick recap, <apex:param> can be inserted into one of the following components :
- <apex:actionFunction>
- <apex:actionSupport>
- <apex:commandLink>
- <apex:outputLink>
- <apex:outputText>
- <flow:interview>
<apex:param> has also got a very useful attribute, which is called “assingTo”. When using this attribute we can assign value of param to static variable in controller just like in the following example:
public static String sortBy { get; set; }
Page :
<apex:pageBlock id ="ThePage">
<apex:commandLink value="link" action="{!action}" reRender="ThePage">
<apex:param name="compareField" value="StartDate" assignTo="{!sortBy}"/>
</apex:commandLink>
</apex:pageBlock>
In the example above, when we click on link, we will asing value of StartDate to static variable sortBy.
But what to do if you want to assign value to non static variable? If you use method presented above you might stumble across some obstacles, therefore in order to do that properly you need to use System class methods and <apex:param> name attribute just like in this example below:
public String residentName { get; set; }
public PageReference sendResidentNameToSensorStatusComponent() {
residentName = System.currentPageReference().getParameters().get('residentName');
return null;
}
Page :
<apex:commandLink value="link" action="{!sendResidentNameToSensorStatusComponent}">
<apex:param name="residentName" value="John"/>
</apex:commandLink>
In this particular example, after clicking <apex:commandLink> we will have, the sample value (in this case it’s John) assigned to ‘residentName’ variable in controller.
Hope it helps! Good luck!