StandardSetController Class

StandardSetController objects allow you to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce. The StandardSetController class also contains a prototype object. This is a single sObject contained within the Visualforce StandardSetController class. If the prototype object's fields are set, those values are used during the save action, meaning that the values are applied to every record in the set controller's collection. This is useful for writing pages that perform mass updates (applying identical changes to fields within a collection of objects).
Note
Fields that are required in other Salesforce objects will keep the same requiredness when used by the prototype object.
Keep in mind the following governor limits for batch Apex:

Instantiation

You can instantiate a StandardSetController in either of the following ways:
  • From a list of sObjects:
    List<account> accountList = [SELECT Name FROM Account LIMIT 20];
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
  • From a query locator:
    ApexPages.StandardSetController ssc = 
    new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));

Methods

StandardSetController methods are all called by and operate on a particular instance of a StandardSetController.

The table below describes the instance methods for StandardSetController.

Name Arguments Return Type Description
cancel System.PageReference Returns the PageReference of the original page, if known, or the home page.
first Void Returns the first page of records.
getCompleteResult Boolean Indicates whether there are more records in the set than the maximum record limit. If this is false, there are more records than you can process using the list controller. The maximum record limit is 10,000 records.
getFilterId String Returns the ID of the filter that is currently in context.
getHasNext Boolean Indicates whether there are more records after the current page set.
getHasPrevious Boolean Indicates whether there are more records before the current page set.
getListViewOptions System.SelectOption[] Returns a list of the listviews available to the current user.
getPageNumber Integer Returns the page number of the current page set. Note that the first page returns 1.
getPageSize Integer Returns the number of records included in each page set.
getRecord sObject Returns the sObject that represents the changes to the selected records.This retrieves the prototype object contained within the class, and is used for performing mass updates.
getRecords sObject[] Returns the list of sObjects in the current page set. This list is immutable, i.e. you can't call clear() on it.
getResultSize Integer Returns the number of records in the set.
getSelected sObject[] Returns the list of sObjects that have been selected.
last Void Returns the last page of records.
next Void Returns the next page of records.
previous Void Returns the previous page of records.
save System.PageReference Inserts new records or updates existing records that have been changed. After this operation is finished, it returns a PageReference to the original page, if known, or the home page.
setFilterID String filterId Void Sets the filter ID of the controller.
setpageNumber Integer pageNumber Void Sets the page number.
setPageSize Integer pageSize Void Sets the number of records in each page set.
setSelected sObjects[] selectedRecords Void Set the selected records.

Example

The following example shows how a StandardSetController object can be used in the constructor for a custom list controller:
public class opportunityList2Con {
  // ApexPages.StandardSetController must be instantiated 
    
  // for standard list controllers 
    
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select name,closedate from Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records 
    
    public List<Opportunity> getOpportunities() {
         return (List<Opportunity>) setCon.getRecords();
    }
}
The following Visualforce markup shows how the controller above can be used in a page:
<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.name}"/>
            <apex:column value="{!o.closedate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.