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
pinal2pinal2 

Understanding StandardController

Hello everyone,

 

I am very new to Force.com. I am currently go through various documentation/article to understand Force.com technology. I have a question on a code snippet (please see below)  from "Visualforce Developer's Guide".

 

My question comes from the line "this.acct = (Account)stdController.getRecord();". How does the StandardController knows to retrieve Account record without providing any information pertaining to Account to StandarController. If for example, if I substitue the casting of (Account) to (Contacts), will I get the record information of Contacts?

 

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();
}
public String getGreeting() {
return 'Hello ' + acct.name + ' (' + acct.id + ')';
}
}

 

 

Thanks for your valuable response in advance!

 

Regards!

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

When you create a page like this:

 

<apex:page standardController="Account" extensions="myControllerExtension">

</apex:page>

and then visit your page, the first thing that happens behind the scenes is an Account standard controller gets instantiated, which is basically an ApexPages.StandardController object whose getRecord will return an sObject of type account.  The account will either be new or an existing account, depending on whether you've specified an account id in your query string.

 

 

When you specify an extension on your page, you almost always need some way to get information off of the record your standard controller is using, so when the controller extension is created, that StandardController object is passed in for you, giving your extension access to your Account record.

 

The example you've posted is for use with a page using the Account standard controller.  You could actually specify this extension on a page that had standardController="Contact" for example, but then getRecord would be returning your contact object, and you'd get an exception as soon as the constructor for your extension is called.

 

Does that make sense?  getRecord will return the sObject of the standardController that is referenced by the page using this extension.

All Answers

jwetzlerjwetzler

When you create a page like this:

 

<apex:page standardController="Account" extensions="myControllerExtension">

</apex:page>

and then visit your page, the first thing that happens behind the scenes is an Account standard controller gets instantiated, which is basically an ApexPages.StandardController object whose getRecord will return an sObject of type account.  The account will either be new or an existing account, depending on whether you've specified an account id in your query string.

 

 

When you specify an extension on your page, you almost always need some way to get information off of the record your standard controller is using, so when the controller extension is created, that StandardController object is passed in for you, giving your extension access to your Account record.

 

The example you've posted is for use with a page using the Account standard controller.  You could actually specify this extension on a page that had standardController="Contact" for example, but then getRecord would be returning your contact object, and you'd get an exception as soon as the constructor for your extension is called.

 

Does that make sense?  getRecord will return the sObject of the standardController that is referenced by the page using this extension.

This was selected as the best answer
pinal2pinal2

Hi Jill,

 

Very nice explanation! Thanks for explaining in detail and also for prompt response.

 

Regards!