stratoflow

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:

Building a new application or extending your development team?

🚀 We're here to assist you in accelerating and scaling your business. Send us your inquiry, and we'll schedule a free estimation call.

Estimate your project
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!

Building a new application or extending your development team?

🚀 We're here to assist you in accelerating and scaling your business. Send us your inquiry, and we'll schedule a free estimation call.

Estimate your project

Testimonials

They have a very good company culture of their own, which gives them a real edge compared to other providers.

CEO

Leading UK system integrator

They're very skilled technically and are also able to see the bigger picture.

Managing Partner

Scalable SaaS for healthcare IoT built on Salesforce platform

They've been consistently able to deliver work on time and within budget.

CTO

High performance SaaS for financial insitutions

We are seriously impressed by the quality and broader picture of anything they do for us.

CEO

Gold trading platform

testowy tekst

Martyna

Senior