StandardSetController Class
Namespace
Usage
Instantiation
- 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
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();
}
}
<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>
StandardSetController Constructors
The following are constructors for StandardSetController.
StandardSetController(queryLocator)
Signature
public StandardSetController(Database.QueryLocator queryLocator)
Parameters
- queryLocator
- Type: Database.QueryLocator
- A query locator representing a list of sObjects.
StandardSetController(controllerSObjects)
Signature
public StandardSetController(List<sObject> controllerSObjects)
Example
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
StandardSetController Methods
The following are methods for StandardSetController. All are instance methods.
cancel()
Signature
public System.PageReference cancel()
Return Value
Type: System.PageReference
first()
Signature
public Void first()
Return Value
Type: Void
getCompleteResult()
Signature
public Boolean getCompleteResult()
Return Value
Type: Boolean
getFilterId()
Signature
public String getFilterId()
Return Value
Type: String
getHasNext()
Signature
public Boolean getHasNext()
Return Value
Type: Boolean
getHasPrevious()
Signature
public Boolean getHasPrevious()
Return Value
Type: Boolean
getListViewOptions()
Signature
public System.SelectOption getListViewOptions()
Return Value
Type: System.SelectOption[]
getPageNumber()
Signature
public Integer getPageNumber()
Return Value
Type: Integer
getPageSize()
Signature
public Integer getPageSize()
Return Value
Type: Integer
getRecord()
Signature
public sObject getRecord()
Return Value
Type: sObject
getRecords()
Signature
public sObject[] getRecords()
Return Value
Type: sObject[]
getResultSize()
Signature
public Integer getResultSize()
Return Value
Type: Integer
getSelected()
Signature
public sObject[] getSelected()
Return Value
Type: sObject[]
last()
Signature
public Void last()
Return Value
Type: Void
next()
Signature
public Void next()
Return Value
Type: Void
previous()
Signature
public Void previous()
Return Value
Type: Void
save()
Signature
public System.PageReference save()
Return Value
Type: System.PageReference
setFilterID(filterId)
Signature
public Void setFilterID(String filterId)
Parameters
- filterId
- Type: String
Return Value
Type: Void
setpageNumber(pageNumber)
Signature
public Void setpageNumber(Integer pageNumber)
Parameters
- pageNumber
- Type: Integer
Return Value
Type: Void
setPageSize(pageSize)
Signature
public Void setPageSize(Integer pageSize)
Parameters
- pageSize
- Type: Integer
Return Value
Type: Void
setSelected(selectedRecords)
Signature
public Void setSelected(sObject[] selectedRecords)
Parameters
- selectedRecords
- Type: sObject[]
Return Value
Type: Void
Usage
Use the setSelected() method in your Apex controller or controller extension to manually set the records displayed on a Visualforce page. The setSelected() method overwrites any previously selected records with the records specified in the selectedRecords argument.
Example
AccountNamePage shows a table of account names. MyControllerExtension’s constructor contains a SOQL query that returns a list of accounts. This list is passed into setSelected() so that the account records in the list are selected and displayed in the table.
<!-- AccountNamePage.page -->
<apex:page standardController="Account" recordSetVar="accounts" extensions="MyControllerExtension">
<apex:pageBlock>
<apex:pageBlockTable value="{!accounts}" var="acc">
<apex:column value="{!acc.name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
// MyControllerExtension.cls
public with sharing class MyControllerExtension {
private ApexPages.StandardSetController setController;
public MyControllerExtension(ApexPages.StandardSetController setController) {
this.setController = setController;
Account [] records = [SELECT Id, Name FROM Account LIMIT 30];
setController.setSelected(records);
}
}