How to access ListView in Apex | Using StandardSetController for Pagination
Example :In this post i am using Account object listviews.
Use ListView in Visualforce with Paging and Navigation
Apex class :
/**
* Description : Controller Class to show How to utilize existing List View in Apex with Pagination Support
*/
public with sharing class ListViewDemo {
private String baseQuery = 'Select ID, Name FROM Account ORDER BY NAME ASC';
public String AccFilterId {get; set;}
private Integer pageSize = 10;
public ListViewDemo(){}
public ApexPages.StandardSetController AccSetController {
get{
if(AccSetController == null){
AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery));
AccSetController.setPageSize(pageSize);
// We have to set FilterId after Pagesize, else it will not work
if(AccFilterId != null)
{
AccSetController.setFilterId(AccFilterId);
}
}
return AccSetController;
}set;
}
public ListViewDemo(ApexPages.StandardSetController c) { }
//Navigate to first Page
public void firstPage()
{
AccSetController.first();
}
//Navigate to last Page
public void lastPage()
{
AccSetController.last();
}
//Navigate to Next page
public void next()
{
if(AccSetController.getHasNext())
{
AccSetController.next();
}
}
//Navigate to Prev Page
public void prev()
{
if(AccSetController.getHasPrevious())
{
AccSetController.previous();
}
}
public List<Account> getAccounts()
{
return (List<Account>)AccSetController.getRecords();
}
//Get all available list view for Account
public SelectOption[] getAccountExistingViews(){
return AccSetController.getListViewOptions();
}
/**
* Reset List View
*/
public PageReference resetFilter()
{
AccSetController = null;
AccSetController.setPageNumber(1);
return null;
}
}
Visualforce Page:
<apex:page controller="ListViewDemo">
Available List Views for Account :
<apex:form id="pageForm">
<apex:selectList value="{!AccFilterId}" size="1" id="filterMenu">
<apex:selectOptions value="{!AccountExistingViews}"></apex:selectOptions>
<apex:actionSupport event="onchange" action="{!resetFilter}" rerender="AccntTable" status="ajaxStatus"/>
</apex:selectList>
<apex:actionStatus id="ajaxStatus" startText="Loading..." stopText=""/>
<apex:pageBlock title="Accounts">
<apex:pageBlockButtons >
<apex:commandButton action="{!firstPage}" value="|<<" reRender="AccntTable" status="ajaxStatus" />
<apex:commandButton action="{!prev}" value="<" reRender="AccntTable" status="ajaxStatus" />
<apex:commandButton action="{!next}" value=">" reRender="AccntTable" status="ajaxStatus" />
<apex:commandButton action="{!lastPage}" value=">>|" reRender="AccntTable" status="ajaxStatus" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!Accounts}" var="item" id="AccntTable">
<apex:column value="{!item.name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Test Class :
@isTest
public class ListViewDemoTest
{
@testSetup
static void createAccount() {
// Create common test accounts
List<Account> testAccts = new List<Account>();
for(Integer i=0;i<20;i++) {
testAccts.add(new Account(Name = 'TestAcct'+i));
}
insert testAccts;
}
public static testMethod void getListView(){
//Lets Assume we are writing Controller extension to use on List View of Account
List <Account> acctList = [SELECT ID FROM Account];
//Check Account created count by setup()
System.assertEquals(20,acctList.size());
//Start Test Context, It will reset all Governor limits
Test.startTest();
//Inform Test Class to set current page as your Page where Extension is used
Test.setCurrentPage(Page.ListViewDemo);
//Instantiate object of "ApexPages.StandardSetController" by passing array of records
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acctList);
//Now, create Object of your Controller extension by passing object of standardSetController
ListViewDemo ext = new ListViewDemo(stdSetController);
SelectOption[] selOptions = ext.getAccountExistingViews();
//We should not assert count of list View as no control over creation of list view
//but in my Dev org, I know count is 6
System.assertEquals(6,selOptions.size());
ext.firstPage();
List<Account> accFirsttPage = ext.getAccounts();
System.assertEquals( 10, accFirsttPage.size() );
ext.next();
ext.prev();
ext.resetFilter();
ext.lastPage();
//Finish Test
Test.stopTest();
}
}
That's it !!!!
1 comments:
Hi Please Explain about CROSS OBJECT FORMULAS
Post a Comment