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:
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class opportunityList2Con {
18 // ApexPages.StandardSetController must be instantiated
19 // for standard list controllers
20 public ApexPages.StandardSetController setCon {
21 get {
22 if(setCon == null) {
23 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
24 [SELECT Name, CloseDate FROM Opportunity]));
25 }
26 return setCon;
27 }
28 set;
29 }
30
31 // Initialize setCon and return a list of records
32 public List<Opportunity> getOpportunities() {
33 return (List<Opportunity>) setCon.getRecords();
34 }
35}The following Visualforce markup shows how the controller above can be used in a page:
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="opportunityList2Con">
18 <apex:pageBlock>
19 <apex:pageBlockTable value="{!opportunities}" var="o">
20 <apex:column value="{!o.Name}"/>
21 <apex:column value="{!o.CloseDate}"/>
22 </apex:pageBlockTable>
23 </apex:pageBlock>
24</apex:page>