Standard Controllers

Every database object, both standard and custom, has a standard controller. Its name is simply the name of the object. No Apex code exists for a standard controller. The controller implementation is already provided by Force.com.

Working with a Single Record

By default, the standard controller operates on a single record at a time. It receives this record from the id parameter in the URL. Try this for yourself by creating a new Visualforce page named MyPage6_3 with the code in Listing 6.3.

Listing 6.3 Visualforce Page Using Standard Controller


<apex:page standardController="Project__c">
  The current project is: {!Project__c.Name}
  <apex:form >
    <apex:commandButton action="{!edit}" value="Edit {!Project__c.Name}" />
    <apex:commandButton action="{!list}" value="Go To List" />
  </apex:form>
</apex:page>


If you visit the page in your browser (/apex/mypage6_3) without providing an id, you’ll see no current project named in the page. If you append an id value for a Project record (for example, /apex/MyPage6_3?id=a008000000CTwEw), you can get the name of the project and working edit and list buttons.

Listing 6.3 demonstrates a few actions provided by the standard controller, leveraged using expression language in view components. For example, access to the current project record is provided through {!Project__c}, and access to the navigation actions through {!edit} and {!list}. In general, the following expressions are available in a page that uses a standard controller:

Image Data—{!id} is the unique identifier of the current record, and {! object} is the current record itself, where object is the lowercase name of your object. All fields of the object are automatically available, including related child objects but not parent objects.

Image Navigation—{!cancel} navigates to the cancel page, {!edit} to the standard edit page, and {!view} to the standard view page.

Image Action and navigation—{!delete} deletes the current record and navigates to the standard delete page, and {!save} saves the current record and refreshes the page.

Image Action only—{!quicksave} saves the current record without navigation.

Working with Multiple Records

A variation of the standard controller exists called the standard set controller. It operates on a list of records rather than a single record. The list is produced by executing a view, a user-defined set of column names, filter criteria, and sort criteria for an object. To try it, create another Visualforce page named MyPage6_4 with the code given in Listing 6.4.

Listing 6.4 Visualforce Page with Standard Set Controller


<apex:page standardController="Project__c" recordSetVar="projects">
  <apex:repeat value="{!projects}" var="p">
    {!p.Name}<br />
  </apex:repeat>
</apex:page>


Visit /apex/myPage6_4 with your browser, and you’ll see a list of all projects. Force.com has used the user’s most recently executed view to obtain a list of project records, sorted by the first column in the view, even if that column is not displayed in the Visualforce page. The records are available to your page in the variable projects, specified by the page attribute recordSetVar. The recordSetVar indicates to Force.com that the standard set controller should be used.

The standard set controller allows you to work with up to 10,000 records at once and supports pagination with a variable page size. It also supports multiple selection and actions on a selected set of records.

The following expressions are valid in any page that uses a standard set controller:

Image Data—The variable name you set in recordSetVar is bound to the current list of records, {!selected} is an array of SObjects that are selected, {!resultsSize} sets or gets the number of records currently displayed, and {!completeResult} is a Boolean containing false if more than 10,000 records exist.

Image Pagination—Navigate across multiple pages of data using the {!first}, {!last}, {!next}, and {!previous} actions. {!pageNumber} sets or gets the current page number, and {!pageSize} sets or gets the number of records in a page. {!hasPrevious} returns true if a previous page exists, and {!hasNext} returns true if a subsequent page exists.

Image Filters—{!filterId} is the unique identifier of the currently selected filter (list view), and {!listViewOptions} is an array of SelectOption objects containing the names and identifiers of the available list views.

Image Navigation—{!cancel} navigates to the cancel page, and {!edit} to the standard edit page.

Image Action and navigation—{!delete} deletes the current record and navigates to the standard delete page, and {!save} saves the current record and refreshes the page.

Image Action only—{!quicksave} saves the current record without navigation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.92.78