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).
Fields that are required in other Salesforce objects will keep the same requiredness when used by the prototype object.
You can instantiate a StandardSetController in either of the following ways:
-
From a list of sObjects:
-
From a query locator:
The maximum record limit for StandardSetController is 10,000 records. Instantiating StandardSetController using a query locator returning more than 10,000 records causes a LimitException to be thrown. However, instantiating StandardSetController with a list of more than 10,000 records doesn’t throw an exception, and instead truncates the records to the limit.
The following example shows how a StandardSetController object can be used in the constructor for a custom list controller:
The following Visualforce markup shows how the controller above can be used in a page:
The following are constructors for StandardSetController.
-
StandardSetController(queryLocator)
Creates an instance of the
ApexPages.StandardSetControllerclass for the list of objects returned by the query locator. -
StandardSetController(controllerSObjects)
Creates an instance of the
ApexPages.StandardSetControllerclass for the specified list of standard or custom objects.
Creates an instance of the ApexPages.StandardSetController class for the list of objects returned by the query locator.
public StandardSetController(Database.QueryLocator queryLocator)
- queryLocator: Type: Database.QueryLocator A query locator representing a list of sObjects.
Creates an instance of the ApexPages.StandardSetController class for the specified list of standard or custom objects.
public StandardSetController(List<sObject> controllerSObjects)
The following are methods for StandardSetController. All are instance methods.
-
Returns the PageReference of the original page, if known, or the home page.
-
Changes the set of records that the controller returns to the first page of records.
-
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.
-
Returns the ID of the filter that is currently in context.
-
Indicates whether there are more records after the current page set.
-
Indicates whether there are more records before the current page set.
-
Returns a list of the listviews available to the current user.
-
Returns the page number of the current page set. Note that the first page returns 1.
-
Returns the number of records included in each page set.
-
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.
-
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call
clear() on it. -
Returns the number of records in the set.
-
Returns the list of sObjects that have been selected.
-
Changes the set of records that the controller returns to the last page of records.
-
Changes the set of records that the controller returns to the next page of records.
-
Changes the set of records that the controller returns to the previous page of records.
-
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.
-
Sets the filter ID of the controller.
-
Sets the page number.
-
Sets the number of records in each page set.
-
Set the selected records to the records specified in the selectedRecords argument.
Returns the PageReference of the original page, if known, or the home page.
public System.PageReference cancel()
Type: System.PageReference
See Also
Changes the set of records that the controller returns to the first page of records.
public Void first()
Type: Void
See Also
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.
public Boolean getCompleteResult()
Type: Boolean
Returns the ID of the filter that is currently in context.
The getFilterID() method doesn’t support list views without filter IDs, such as the Recently Viewed list view. In these cases, the method returns the first filter ID of the object’s available list views. If called within an <apex:enhancedList> component, the method returns the filter ID of the last used list view.
public String getFilterId()
Type: String
See Also
- Visualforce Developer Guide: Standard List Controller Actions
- Visualforce Developer Guide: List Views with Standard List Controllers
Indicates whether there are more records after the current page set.
public Boolean getHasNext()
Type: Boolean
Indicates whether there are more records before the current page set.
public Boolean getHasPrevious()
Type: Boolean
Returns a list of the listviews available to the current user.
public System.SelectOption getListViewOptions()
Type: System.SelectOption[]
See Also
- Visualforce Developer Guide: Standard List Controller Actions
- Visualforce Developer Guide: List Views with Standard List Controllers
Returns the page number of the current page set. Note that the first page returns 1.
public Integer getPageNumber()
Type: Integer
Returns the number of records included in each page set.
public Integer getPageSize()
Type: Integer
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.
public sObject getRecord()
Type: sObject
See Also
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call clear() on it.
public sObject[] getRecords()
Type: sObject[]
See Also
Returns the number of records in the set.
public Integer getResultSize()
Type: Integer
Returns the list of sObjects that have been selected.
public sObject[] getSelected()
Type: sObject[]
Changes the set of records that the controller returns to the last page of records.
public Void last()
Type: Void
See Also
Changes the set of records that the controller returns to the next page of records.
public Void next()
Type: Void
See Also
Changes the set of records that the controller returns to the previous page of records.
public Void previous()
Type: Void
See Also
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.
public System.PageReference save()
Type: System.PageReference
See Also
Sets the filter ID of the controller.
public Void setFilterID(String filterId)
- filterId: Type: String
Type: Void
Sets the page number.
public Void setpageNumber(Integer pageNumber)
- pageNumber: Type: Integer
Type: Void
Sets the number of records in each page set.
public Void setPageSize(Integer pageSize)
- pageSize: Type: Integer
Type: Void
Set the selected records to the records specified in the selectedRecords argument.
public Void setSelected(sObject[] selectedRecords)
- selectedRecords: Type: sObject[]
Type: Void
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.
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.
See Also