Salesforce List Controller – Standard and Custom List Controller

FREE Online Courses: Knowledge Awaits – Click for Free Access!

1. Objective

In this Salesforce List Controller Tutorial, we are about to learn what does a List controller mean. Moreover, we will discuss how to build a Custom List Controller and using Standard Salesforce List Controller.
So, let’s start Salesforce List Controller.

Salesforce List Controller - Standard and Custom List Controller

Salesforce List Controller – Standard and Custom List Controller

Do you know about Salesforce Objects

2. What is Salesforce List Controller?

Salesforce list controller is nothing but a controller which assist and helps you to create Visualforce pages that can act on a set of records. For an instance all the pages in Salesforce that work with the set of records like list pages, mass action pages, and related lists. It is used for the following

  • Account
  • Campaign
  • Asset
  • Case
  • Contract
  • Lead
  • Idea
  • Opportunity
  • Product2
  • Order
  • Solution
  • Custom objects
  • User

3. Building a Custom List Controller in Salesforce

A costume list controller and standard list controller are similar to each other. Costume list controller has the power to implement apex logic that is defined to act on the set of records.

public class opportunityList2Con {
   // ApexPages.StandardSetController must be instantiated
   // for standard list controllers
   public ApexPages.StandardSetController setCon {
       get {
           if(setCon == null) {
               setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                   [SELECT Name, CloseDate FROM Opportunity]));
           }
           return setCon;
       }
       set;
   }
   // Initialize setCon and return a list of records
   public List<Opportunity> getOpportunities() {
       return (List<Opportunity>) setCon.getRecords();
   }
}

A costume list controller can also be created that uses anti- and semi-joins as one of the parts of SOQL query. It can be done in the following ways.
Have a look at Salesforce Environment

public with sharing class AccountPagination {
   private final Account acct;
   // The constructor passes in the standard controller defined
   // in the markup below
   public AccountPagination(ApexPages.StandardSetController controller) {
       this.acct = (Account)controller.getRecord();
   }
   public ApexPages.StandardSetController accountRecords {
       get {
           if(accountRecords == null) {
               accountRecords = new ApexPages.StandardSetController(
                   Database.getQueryLocator([SELECT Name FROM Account WHERE Id NOT IN
                       (SELECT AccountId FROM Opportunity WHERE IsClosed = true)]));
           }
           return accountRecords;
       }
       private set;
   }
   public List<Account> getAccountPagination() {
        return (List<Account>) accountRecords.getRecords();
   }
}

The page that shows all these records uses a set of standard list controller actions, still, it all depends on iterating over the records returned.

<apex:page standardController="Account" recordSetVar="accounts" extensions="AccountPagination">
   <apex:pageBlock title="Viewing Accounts">
       <apex:form id="theForm">
           <apex:pageBlockSection >
               <apex:dataList value="{!accountPagination}" var="acct" type="1">
                   {!acct.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>

4. Using Standard List Controller

Action strategies perform logic or navigation once a page event happens, like once a user clicks a button, or hovers over an area of the page. Action strategies is referred to as from page markup by using notation within the action parameter of 1 of the following tags:
Do you know about Salesforce Export Data

SaveInserts new records or updates existing records that are modified. after this operation is finished, the save action returns the user to the initial page, if known, or the home page.
Quick Save Inserts new records or updates existing records that are modified. in contrast to the save action, quicksave doesn’t redirect the user to a different page.
ListReturns a PageReference object of the standard list page, based on the foremost recently used list filter for that object once the filtered isn’t specified by the user.
CancelAborts an edit operation. when this operation is finished, the cancel action returns the user to the page where the user originally invoked the edit.
FirstDisplays the primary page of records within the set.
LastDisplays the last page of records within the set.
NextDisplays consecutive page of records within the set.
PreviousDisplays the previous page of records within the set.

So, this was all in the Salesforce List Controller. Hope you like our explanation.

5. Conclusion: Salesforce List Controller

In this Salesforce List Controller tutorial, we learned what does the term list controller in Salesforce means. Moreover, we discussed building a custom list controller and using standard list controller. still, if any doubt occurs, ask in the comment tab.
See also –
Role Hierarchy in Salesforce
For reference

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *