Tuesday 15 September 2015

Standard List Controller Pagination-Part # 1

This post explains you the first level of pagination in salesforce using Standard list controller.

Standard list controller

Standard list controllers allow you to create Visualforce pages that can display set of records. List views, related lists, mass action pages are examples of existing Salesforce pages that work with a set of records.

Standard list controllers can be used with the following objects:

1. Account
2. Asset
3. Campaign
4. Case
5. Contact
6. Contract
7. Idea
8. Lead
9. Opportunity
10. Order
11. Product2
12. Solution
13. User
14. All Custom objects

Using a standard list controller is very similar to using a standard controller. First you set the standardController attribute on the <apex:page> component, then you set the recordSetVar attribute on the same component.

For example, to associate a page with the standard list controller for products, use the following markup:



<apex:page standardController="Product2" recordSetvar="products">

Accessing data with List Controllers
Once you have associated a page with a list controller, you can refer to the set of records using visualforce expressions. For example, to create a simple table of products, create a page with the following markup:

Visualforce Page : PaginationTutorial_1


<apex:page standardController="Product2" recordSetvar="products">
<apex:form >
<apex:pageBlock title="Product Pagination using Standard List Controller">
    <apex:pageBlockButtons location="bottom">
        <div style="float:center;font-weight:bold;">
            <apex:commandLink action="{!first}">First</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!previous}">Previous</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!next}">Next</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!last}">Last</apex:commandlink> &nbsp;&nbsp;
        </div>
    </apex:pageBlockButtons>
      <apex:pageBlockTable value="{!products}" var="v">
          <apex:column value="{!v.name}"/>
          <apex:column value="{!v.IsActive}"/>
          <apex:column value="{!v.ProductCode}"/>
          <apex:column value="{!v.RecordTypeId}"/>
          <apex:column value="{!v.CreatedbyId}"/>
          <apex:column value="{!v.CreatedBy.ProfileId}"/>      
          <apex:column value="{!v.CreatedDate}"/>
      </apex:pageBlockTable>           
</apex:pageBlock>
</apex:form>
</apex:page>


Preview


Standard List Controller Pagination-Part # 1

Points to Note
  • When you use the standardController attribute on the <apex:page> tag, you cannot use the controller attribute at the same time
  • The recordSetVar attribute not only indicates that the page uses a list controller, it indicates the variable name of the record collection. This variable can then be used to access data from the record collection
  • This page does not specify a filter in the request, so the page is displayed is based on the last used filter. To check this behavior, reload your visual force page and go to the Products tab and you can observe that records displayed in first page on both these tables are same
  • When using a standard list controller, the returned records sort on the first column of data, as defined by the current view, even if that column is not rendered. More details on "Using List Views with Standard List Controllers" was discussed in Pagination using List Views with Standard List Controllers - Part # 2.
  • Using visualforce expressions, we can access data from the records and embed to the page
  • We can traverse up to five levels of child-to-parent relationships and one level of parent-to-child relationships (Refer to Column "Profile" in screenshot for example)
  • By default, a list controller returns 20 records on the page


Pros
  • No Apex Class required and hence no test class
  • Minimal effort for development
  • No SOQL queries issued to sfdc server (at-least explicitly)
  • Governor limits are not in scope for this page. However the view state limit and heap size limit still applies but will be minimal always as only lesser records are displayed in each page

Cons
  • No more than 10,000 records can be returned by a standard list controller. Custom controllers can work with larger results sets
  • No table sorting on records
  • No Search functionality
  • No customizations to column headers like changing label, css etc.,(Refer to Column "Profile" in screenshot for example, which suppose to be renamed as "Created User Profile" instead for better understanding)
  • You cannot filter records to be displayed inside the table dynamically through code as it was controlled by the list view



No comments:

Post a Comment