2

In this canonical example for constructors, can someone walk me through a couple of questions?

  1. why is the acct private and final?
  2. how are you supposed to use the acct object?
  3. what's going on in the constructor?

Code:

public class myControllerExtension {

private final Account acct;

// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public myControllerExtension(ApexPages.StandardController stdController) {
    this.acct = (Account)stdController.getRecord();
}
5

1) The field acct has been marked as private so that this specific reference to the account record (note "reference", not the account record itself) is not accessible anywhere outside of this instance of the class. It has been marked as final so that, for the scope of this class instance, it will always refer to the same Account record. The final keyword is not necessary, but it prevents some other block of code in this class from changing which Account record acct points to.

Skipping to 3) Most likely, in your visualforce page, you are accessing the fields on the account via merge fields like {!Account.Name}. The StandardController has the method getRecord(), which will provide a reference to the generic sObject that you are accessing from your page, which you can cast to the expected type. You can then store this casted reference in your private final field (provided you haven't already assigned something to acct, of course).

2) You can now use acct to modify the fields on the account record that you are accessing through your page. Any changes you make should be visible on your page after you refresh or reRender the section where the field is displayed. It should be noted however, that your extension can only access/modify fields on the Account which are used in merge fields in the visualforce page. In order to access other fields, you will need to call the StandardController's addFields method prior to getting the record. http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm

You should also always wrap your call to addFields with a check for Test.isRunningTest(), like so:

if (!Test.isRunningTest()) { stdController.addFields(...); }

| improve this answer | |
  • 2
    "2) You can now use acct to modify the fields on the account record" - not when it's private, final and doesn't have getter/setter attached... – eyescream Jun 1 '13 at 8:28
  • 2
    You can't reference acct from the page, but you absolutely can reference it within this class. The final keyword only prevents you from changing the record that acct points to, not the values of the fields on the record. Add a commandButton to the page, tie it to an action in the extension that modifies some value displayed on the page, and return null. You'll see the modified value on the page. – Thomas Crouse Jun 3 '13 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.