What is the difference between list controllers and set controllers?
1 Answer
The two controllers that are available to you are:
- StandardController
- StandardSetController
StandardController
From the docs: A Visualforce controller is a set of instructions that specify what happens when a user interacts with the components specified in associated Visualforce markup, such as when a user clicks a button or link. Controllers also provide access to the data that should be displayed in a page, and can modify component behavior.
You can find more information here, but basically a
StandardController
is for ONE record, i.e. if you'd like to create a new Visualforce page for a single record, you'd use a Standard Controller in your Apex. For example:
public with sharing class MyController {
private ApexPages.StandardController controller;
public MyController(ApexPages.StandardController controller) {
this.controller = controller;
}
// maybe this gets called from a button within your VF page
public void doSomethingWithSingleRecord() {
SObject record = controller.getRecord();
record.put('Status__c', true);
update record;
}
}
StandardSetController
From the docs: Standard list controllers allow you to create Visualforce pages that can display or act on a set of records. Examples of existing Salesforce pages that work with a set of records include list pages, related lists, and mass action pages.
You can find more information here, but basically Set (list) controllers are for MULTIPLE (or a list of) records, i.e. if you'd like to create a new Visualforce page for a List of records (or even from a selection of records on a List view), you'd use a Standard Set Controller in your Apex. For example:
public with sharing class MyController {
private ApexPages.StandardSetController setController;
public MyController(ApexPages.StandardSetController setController) {
this.setController = setController;
}
//maybe this gets called from a button on your list view
public void doSomethingWithMultipleRecords() {
List<SObject> records = setController.getSelected();
for(SObject record : records) {
record.put('Status__c', true);
}
update records;
}
}