Introduction to List Controller
In this section, we will discuss List Controllers(recordSetVar) of Salesforce. At a given time, we need to view a given set of records from a Salesforce object. This can be achieved by using list controllers which allow you to create Visualforce pages and then you can display or act on a set of records. The standard list controllers can be used in the following set of Salesforce objects.
- Account
- Asset
- Campaign
- Case
- Contact
- Contract
- Idea
- Lead
- Opportunity
- Order
- Solution
- User
- Salesforce Custom objects
Example
Here we are taking the example of contact object. We fetch the records from contact object and display it using list controllers. To achieve this, we create a visualforce page with the following code. The code creates a page lock with column values matching the column names of the contact object and then display the records.
Note: List controller, we used to achieve pagination too. By default it will show 20 records per page.
<apex:page standardController="contact" recordSetVar="cnt" > <apex:form > <apex:pageBlock > <apex:pageblockTable value="{!cnt}" var="c"> <apex:column value="{!c.name}"/> </apex:pageblockTable> <apex:commandButton value="<" action="{!first}"/> <apex:commandButton value="<<" action="{!previous}"/> <apex:commandButton value=">" action="{!next}"/> <apex:commandButton value=">>" action="{!last}"/> </apex:pageBlock> </apex:form> </apex:page>
On clicking on preview the page, we get the following output.