Record ID and Data Retrieval from a VisualForce Controller Extension without a SOQL statement

26 Nov

Retrieve field data for a record using the Standard Controller object in a Controller Extension class

public with sharing class MyController {.
  Account MyAccount;
  Id MyId;

  public MyController(ApexPages.StandardController controller) {
    list MyFieldList;  
    
    // get the record
    // passing a list of field names to the standard controller will cause 
    // the standard controller to retrieve the field data of the record
    MyFieldList = new list{'Id', 'Name', 'BillingCity', 'BillingState', 'MyCustomField__c'};
    controller.addFields(MyFieldList);
    MyAccount= (Account)controller.getRecord();

     // another way to get more fields. use the addFields method
     // by default only fields referenced in the VisualForce page are retrieved.
     controller.addFields(new list{'ShippingCity', 'ShippingState'});

     // get the record id
     // this is the record id of the query string in the URL
     MyId = controller.getId()
  }
}

Salesforce Documentation:
Standard Controller Class
StandardController Instance Methods