Newer Version Available
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.
Namespace
ApexPages
Usage
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).
Instantiation
You can instantiate
a StandardSetController in either of the following ways:
- From a list of sObjects:
1List<account> accountList = [SELECT Name FROM Account LIMIT 20]; 2ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList); - From a query locator:
1ApexPages.StandardSetController ssc = 2new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));
Example
The following example shows how
a StandardSetController object can be used in the constructor for
a custom list controller:
1public class opportunityList2Con {
2 // ApexPages.StandardSetController must be instantiated
3 // for standard list controllers
4 public ApexPages.StandardSetController setCon {
5 get {
6 if(setCon == null) {
7 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
8 [SELECT Name, CloseDate FROM Opportunity]));
9 }
10 return setCon;
11 }
12 set;
13 }
14
15 // Initialize setCon and return a list of records
16 public List<Opportunity> getOpportunities() {
17 return (List<Opportunity>) setCon.getRecords();
18 }
19}The following Visualforce markup shows how the controller above can be used in a page:
1<apex:page controller="opportunityList2Con">
2 <apex:pageBlock>
3 <apex:pageBlockTable value="{!opportunities}" var="o">
4 <apex:column value="{!o.Name}"/>
5 <apex:column value="{!o.CloseDate}"/>
6 </apex:pageBlockTable>
7 </apex:pageBlock>
8</apex:page>