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
Tan JTan J 

Is apex getter method runs only once regardless of how many times it is called on VF page?

 I have a basic question. I have a big VF page with many sections which render/hide based on some flags. The flags are set in constructor of controller extension and are mostly depend on the logged-in user profile, role etc. (i.e. they do not change value). Now I am trying to make my constructor thin and move these flags outside it in getter/setter methods. However, I want to know if these getters will always be called when they are referenced in 'rendered' on multiple sections etc. (i.e. the flags are referred in VF page multiple times). When I did some testing, it looks like they are called only first time but want to confirm my understanding. With the flags being set in constructor, I am assured that they will run only once when the page loads. But I want to reduce the code in constructor.
Best Answer chosen by Tan J
Shashi PatowaryShashi Patowary
Hi Tan,
It's possible that getter method may be exceuted multiple times.Let's say that the VF page  has link or button (CommandButton) and on clicking a controller method is called.In this case getter method is called once again before the page loads.That's why one of VF page best practices is to avoid SOQL within getter method. It degrades the performance of the page.

Avoid SOQL queries in your Apex controller getter methods.

In your case, if the flags need to be set only once in the page and these remains constant throughout the interaction in the page, these should be initialized inside the controller.

Please let me know if you get any helpful information on this.

Regards,
Shashi
 

All Answers

Shashi PatowaryShashi Patowary
Hi Tan,

If your getter variables are dependent on each other, it may lead to issue. Here is an interesting topic on this -

http://www.tehnrd.com/getter-method-order-and-visualforce-pages/

Regards,
Shashi
Tan JTan J
Yes, I have gone though that post. It does not answer whether getters run multiple times. My getter variables are not dependent on each other. I just want to know if they will run every time they are accessed from vf page or they will only run it first time and retain their value for future calls.
Shashi PatowaryShashi Patowary
Hi Tan,
It's possible that getter method may be exceuted multiple times.Let's say that the VF page  has link or button (CommandButton) and on clicking a controller method is called.In this case getter method is called once again before the page loads.That's why one of VF page best practices is to avoid SOQL within getter method. It degrades the performance of the page.

Avoid SOQL queries in your Apex controller getter methods.

In your case, if the flags need to be set only once in the page and these remains constant throughout the interaction in the page, these should be initialized inside the controller.

Please let me know if you get any helpful information on this.

Regards,
Shashi
 
This was selected as the best answer
Tan JTan J
Thanks Shashi for the replies. I posted same question on stack exchange here (http://salesforce.stackexchange.com/questions/97059/is-apex-getter-method-runs-only-once-regardless-of-how-many-times-it-is-called-o).
There are two ways: 1) Use constructor or 2) use getter/setter properties with lazy initialization. I am going to implement the second way simply because I want less code in constructor.  Please share your thoughts.
 
Shashi PatowaryShashi Patowary
Yes Tan, using getter with lazy initialization is the best approach/practice for performance of a VF page especially when getter method returns a static kind of data or it has a SOQL query (it should be avoided as much as possible).
Additionally, you can enable developer mode and View View state flag to monitor your VF page for performance.

Regards,
Shashi