Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Selecting Records with a Visualforce Custom List Controller

Selecting Records with a Visualforce Custom List Controller

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

Problem

You need to edit a set of records, but you want to exclude irrelevant ones.

Solution

Create a Visualforce page using a custom list controller. The custom list controller can define which records to present using SOQL.

Discussion

A custom list controller is similar to a standard list controller, except it implements Apex logic to define actions on a set of records.

You can also create a custom list controller that uses anti- and semi-joins as part of the SOQL query. Anti-joins exclude records that match certain criteria, while semi-joins includes records.

The following custom list controller uses an anti-join to retrieve all accounts that don't have any open opportunities. It is implemented as a controller extension:
public with sharing class AccountPagination {
    private final Account acct;  

    public AccountPagination(
           ApexPages.StandardSetController controller) 
    {
           this.acct = (Account)controller.getRecord(); 
    }    
    
    public ApexPages.StandardSetController accountRecords{
        get {
            if(accountRecords == null) {
                return new ApexPages.StandardSetController(
                         Database.getQueryLocator(
                [SELECT name FROM Account WHERE Id NOT IN 
                (SELECT AccountId FROM Opportunity 
                 WHERE IsClosed = false)]));
            }
            return accountRecords;
        }
        private set;
    }
    public List<Account> getAccountPagination() {
         return (List<Account>) accountRecords.getRecords();
    }  
}
The page to display these records uses standard list controller actions ({!previous} and {!next}). However, the set of available records depends on the list returned from the custom list controller:
<apex:page standardController="Account" recordSetvar="accounts" 
           extensions="AccountPagination">
  <apex:pageBlock title="Viewing Accounts">
  <apex:form id="theForm">
    <apex:pageBlockSection >
      <apex:dataList var="a" value="{!accountPagination}" type="1">
        {!a.name}
      </apex:dataList>
    </apex:pageBlockSection>
    <apex:panelGrid columns="2">
      <apex:commandLink action="{!previous}">
          Previous
      </apex:commandlink>
      <apex:commandLink action="{!next}">
          Next
      </apex:commandlink>
    </apex:panelGrid>
  </apex:form> 
  </apex:pageBlock>
</apex:page>

Share

Recipe Activity - Please Log in to write a comment

This is not working. I copied the code as it is and next and previous button are not working.

by Kalyan Kuchi  (2015-03-21)

a

voted as verified by amit sahu  (2011-06-18)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.