function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick DixonPatrick Dixon 

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.

ericszulcericszulc

Maybe use a component with attributes assigned to a variable in the controller? 

Damien_Damien_

<apex:variable value="{!setthisintrigger}" />

Patrick DixonPatrick Dixon

<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.

Mats ErikssonMats Eriksson

Any solution to this problem yet Patrick?

 

/Mats

Patrick DixonPatrick Dixon

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.

Damien_Damien_

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.

Mats ErikssonMats Eriksson

According to the documentation it should work this way:

 

<!-- Page: -->  
    
<apex:page controller="variableCon">
    <apex:variable var="c" value="{!contact}" />
        
    <p>Greetings, {!c.LastName}.</p>
</apex:page>

/*** Controller ***/  
    
public class variableCon {
    Contact contact;

    public Contact getContact() {
        if (contact == null){                      
        contact = [select LastName from Contact where 
            id = :ApexPages.currentPage().getParameters().get('id')];
        }
        return contact;
    }
}

 

Patrick DixonPatrick Dixon

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.