Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Write Efficient Getter Methods
To reduce the processing load of each request, cache the value of a property calculation so that additional calls can access the property without recalculating the value.
You can also configure the getter methods in your Apex classes to only query for data if the object is null. For example, the following code snippet returns the Account record associated with the page. On the first method call, the method queries for the record because the MyAccount object is null. On subsequent calls, it returns the object’s stored value, which prevents additional identical SELECT queries:
1
2 Account MyAccount;
3 public Account getMyAccount() {
4 if (MyAccount == null) {
5 MyAccount = [SELECT name, annualRevenue FROM Account
6 WHERE
7 id = :ApexPages.currentPage().getParameters().
8 get('id')];
9 }
10 return MyAccount;
11 }
12