2

Suppose I have this URL where a VF page points to a record: https://c.cs16.visual.force.com/apex/oppTest?id=006a0000012Wb4s . In my VF below, I get that the standard controller gets that record from the sObject. In my VF page, what are the limits to what I can access? Standard fields, and custom ones seem to be ok. It also lets me traverse relationships ( say from Opp --> Account--> User ...). Can I also get children records? if so how?

<apex:page standardController="Opportunity" >
<apex:outputField value="{!Opportunity.name}"/>
<apex:outputField value="{!Opportunity.Account.createdby.name}"/>
<apex:outputField value="{!Opportunity.Account.createdby.custom__c}"/>
<apex:outputField value="{!Opportunity.CreatedBy.name}"/>

</apex:page>

2 Answers 2

3

StandardController, or, more formally, ApexPages.StandardController, is a system class that exposes functions for various things you'd want to do with a record.

It can add additional fields to the default record query (addFields) or reset the internal state (reset), return to the last page without performing any action (cancel), save and go on (save), return an edit page (edit), get the record (getRecord) or record's ID (getId), and return a link to the detail page (view).

The primary purpose of this class is to emulate UI actions programmatically, and extend the query parameters for an extension class in a Visualforce page. StandardController is responsible for building the SOQL that will eventually be used by the Visualforce page to render the page.

Normal SOQL limits apply, meaning you can only go "up" four relationships, and "down" one relationship. This means you can't get the parent's parent's parent's parent's parent's name, nor can you get grandchildren records. Note that you don't explicitly specify which relationships you want to query normally, as they are derived from the Visualforce code.

See the documentation for StandardController for further explanation of this class.

Curiously, the last time I played around with this class, I noticed that calling save() does not increase the DML rows or DML count for a transaction, but saving records this way is inefficient and not recommended for bulk saving.

7
  • WHA?!? No DML consumed? Crazy! Not really applicable in any bulk scenario though, as you mentioned.
    – Adrian Larson
    Commented Feb 12, 2015 at 22:50
  • Dude, standard controller is first a ObjectName.class and then it is wrapped into a ApexPages.StandardController class... That is why to instantiate a class that is supposed to be in terminology of salesforce and "extension" you pass a "Standard Controller" aka class wrapper for the object... this way you end up with new classname_blah (new ApexPages.StandardController (SomeObjectClass.class)) Commented Feb 13, 2015 at 2:06
  • @angry_at_russia See System.assert(Opportunity.class.newInstance() instanceof SObject); and System.assert(Opportunity.class instanceof Type); as examples. Saying that StandardController "wraps" an SObject is certainly false. It simply provides methods for doing things with an SObject that you'd do through the UI. Extensions, in Visualforce nomenclature, "extends" the functionality of the page, not the main controller (of which may be StandardController or any custom class you like).
    – sfdcfox
    Commented Feb 13, 2015 at 16:44
  • @sfdcfox thank you. Does it give you all the fields of that record and the relationsips 4 up and 1 down? If that's all stored in the controller, do you get all that from this.acct = (Account)controller.getRecord()? If so why would you need addFields();? .... I'm missing some connections... Commented Feb 13, 2015 at 18:06
  • @nivyaj No, only fields that are referenced in the Visualforce page or addFields are available from the controller. Querying everything would be too expensive and slow down loading the page.
    – sfdcfox
    Commented Feb 13, 2015 at 18:10
1

Standard Controller is a class behind the scenes that wraps the object of the same name. So for Opportunity object it is Opportunity.class

It has all fields and basic methods like {!save}, {!cancel}, {!quicksave} etc. This class is not available to view as other classes, but it can be "extended" with extension class and methods can be overridden for the VF page.

As far as child objects go try:

<apex:repeat value="{!Opportunity.relationshipname}" var="r">
  {!r.field}
</apex:repeat>
4
  • You can get relationship name from the a lookup field to Opportunity on the related object. Commented Feb 12, 2015 at 21:09
  • Opportunity.class is a Type that represents an SObject, not an ApexPages.StandardController. It cannot be "extended", nor can its methods be "overridden".
    – sfdcfox
    Commented Feb 12, 2015 at 22:44
  • so you are saying if your extension has a pagereference method called save it does not override standard save method? Commented Feb 13, 2015 at 2:01
  • The word "extended" is clearly in quotes here :) Just in case there are some real java gurus here... Commented Feb 13, 2015 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .