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
jg@hhsjg@hhs 

Assign controller variable value in Visualforce page on page load

We're using Visualforce pages more and more here, but we've been working around something for a while that I think is probably pretty simple.

 

Many times, we have multiple visualpage pages that use different controllers (or extensions) based on the record type, or how many records we want to pull. 

 

I see there are several different examples using javascript / params to have a user pass a variable value to a controller - we've been successful there.  But I would simply like to set the variable value when the page loads, so for instance if they go to a page that is supposed to display only 30 records, verses a page that is supposed to display 180 records.  Is there a simple way to pass this value to a controller variable using the <apex:page action=""> perhaps?

Best Answer chosen by Admin (Salesforce Developers) 
miha198206miha198206

Hi,

 

You can transfer records count to your VF page through url parameters. Something like /apex/YourVFPage?recordsCount=120

 

And then in controller you can get the records count by this:

 

 

String recordsCount = ApexPages.currentPage().getParameters().get('recordsCount');

 

 

Hope it will help.

 

Regards,

Mikhail

All Answers

Jeremy_nJeremy_n

This is a really handy place to use Custom Settings. You can set up a Custom Settings "object" for view preferences or whatever, and call them up inside the controller without spending a query. You can specify Custom Settings according to User, and even share preferences across pages.

 

This is one of those underutilized areas of Salesforce, IMO. Otherwise, all that trivial data gets stuck on some SObject somewhere, or just not saved because it's seemingly trivial. But storing view preferences goes a long way toward making someone's experience better, without really doing anything complicated.

 

Let me know if you would like examples; the Developers Guide does a pretty good job of explaining.

 

Jeremy

jg@hhsjg@hhs

After a little research on Custom Settings, I'm thinking this is a great feature that I wasn't aware of at all.  I'm not sure if it's going to help in this instance though, because I'm still trying to set the variable value based on the page a User goes to, not the Users themselves.

 

For example:

 

My elders would like one custom button on a page layout that pulls up a visualforce page report that shows 30 associated records, and renders to pdf.  No problem.  They'd also like another button next to it that pulls up the same visualforce page, except showing 180 records, and renders to pdf.  Right now I'm stuck with using two different pages (that's ok), but two different controller extensions just to specify how many records to pull.

 

Am I missing your point of using Custom Settings?

miha198206miha198206

Hi,

 

You can transfer records count to your VF page through url parameters. Something like /apex/YourVFPage?recordsCount=120

 

And then in controller you can get the records count by this:

 

 

String recordsCount = ApexPages.currentPage().getParameters().get('recordsCount');

 

 

Hope it will help.

 

Regards,

Mikhail

This was selected as the best answer
jg@hhsjg@hhs

Ha - too good to be true.  Thanks - that will work perfectly!!