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:
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]));
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>