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.

ApexPages

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.

Creates an instance of the ApexPages.StandardSetController class for the list of objects returned by the query locator.

public StandardSetController(Database.QueryLocator queryLocator)

Creates an instance of the ApexPages.StandardSetController class for the specified list of standard or custom objects.

public StandardSetController(List<sObject> controllerSObjects)

  • controllerSObjects: Type: List<sObject> A List of standard or custom objects.

The following are methods for StandardSetController. All are instance methods.

  • cancel()

    Returns the PageReference of the original page, if known, or the home page.

  • first()

    Changes the set of records that the controller returns to the first page of records.

  • getCompleteResult()

    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.

  • getFilterId()

    Returns the ID of the filter that is currently in context.

  • getHasNext()

    Indicates whether there are more records after the current page set.

  • getHasPrevious()

    Indicates whether there are more records before the current page set.

  • getListViewOptions()

    Returns a list of the listviews available to the current user.

  • getPageNumber()

    Returns the page number of the current page set. Note that the first page returns 1.

  • getPageSize()

    Returns the number of records included in each page set.

  • getRecord()

    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.

  • getRecords()

    Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call clear() on it.

  • getResultSize()

    Returns the number of records in the set.

  • getSelected()

    Returns the list of sObjects that have been selected.

  • last()

    Changes the set of records that the controller returns to the last page of records.

  • next()

    Changes the set of records that the controller returns to the next page of records.

  • previous()

    Changes the set of records that the controller returns to the previous page of records.

  • save()

    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.

  • setFilterID(filterId)

    Sets the filter ID of the controller.

  • setpageNumber(pageNumber)

    Sets the page number.

  • setPageSize(pageSize)

    Sets the number of records in each page set.

  • setSelected(selectedRecords)

    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

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

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)

Type: Void

Sets the page number.

public Void setpageNumber(Integer pageNumber)

Type: Void

Sets the number of records in each page set.

public Void setPageSize(Integer pageSize)

Type: Void

Set the selected records to the records specified in the selectedRecords argument.

public Void setSelected(sObject[] selectedRecords)

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