You need to sign in to do that
Don't have an account?
Passing a static variable from a VF page to an apex controller
This ought to be simple, but I can't figure out how to do it - I want to pass an integer number from a VF page to a custom controller to use in a SOQL query.
Something like this-
public News_Item__c[] getHeadlines() {
String recordsCount = ApexPages.currentPage().getParameters().get('recordsCount');
Integer irecordsCount = integer.valueof(recordsCount);
return [select Name,
id,
Release_Date__c,
Title__c,
Synopsis__c,
Item__c from News_Item__c order by Release_Date__c desc limit :irecordsCount];
}
However, the recordsCount String/Integer needs to be set statically in the VF page so that I can call different numbers of records using the same controller method in different situations.
I don't seem to be able to use apex:param, because I'm not clicking any buttons or inputing anything - I just want to set the variable in the page markup.
Thanks in advance.
Maybe use a component with attributes assigned to a variable in the controller?
<apex:variable value="{!setthisintrigger}" />
<apex:variable var="recordsCount" value="2"/>
in VF page
and
Integer irecordsCount = integer.valueof(!recordsCount);
in the controller, gives a "variable does not exist" error when saving the controller - so it's not passed across.
Any solution to this problem yet Patrick?
/Mats
No I couldn't find a way to do it.
What you can do that might work for you, is to create a custom object to hold the static variable(s), and then use the page name/environment to grab it within the controller.
To be honest, I fail to see what is wrong with your first idea. If you set it in the webpage parameter, that method should work. Especially if you are looking for a static value.
According to the documentation it should work this way:
But that's passing the variable from the controller to VF.
It's not ideal passing variables in the URL when you are using sites and you care about how the URL looks, which is why I sugested a custom object with record(s) accessed via the plain URL.