{"id":3598,"date":"2013-11-08T12:01:25","date_gmt":"2013-11-08T06:31:25","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3598"},"modified":"2015-08-18T05:19:42","modified_gmt":"2015-08-18T05:19:42","slug":"listview-filter-in-apex-with-paging-and-navigation","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/listview-filter-in-apex-with-paging-and-navigation\/","title":{"rendered":"How to access ListView in Apex | Using StandardSetController for Pagination"},"content":{"rendered":"<p style=\"text-align: justify;\">There are scenario in project lifecycle where developer creates SOQL or Dynamic SOQL to return expected result. Also if requirement changes they go back and change existing code to reflect updated SOQL. If you are good developer and avoid to change Apex code at most you will save your SOQL in &#8220;Custom Settings&#8221; and utilize in Dynamic SOQL.<\/p>\n<p style=\"text-align: justify;\">However, There is very good design Developer can suggest to client. Instead of going to your code and changing code for new requirement, how it sounds if we can utilize existing List View inside Apex or Visualforce only \ud83d\ude42 ? It will solve lots of problems and if requirement changed, just change or create new Listview and it will reflect in your Visualforce code. We can almost have same functionality like Navigation buttons and Paging without writing any complex code logic, thanks to <strong>StandardSetController\u00a0<\/strong>capabilities.<\/p>\n<p style=\"text-align: justify;\">There is also very <a title=\"Paginating Data for Force.com Applications\" href=\"https:\/\/developer.salesforce.com\/page\/Paginating_Data_for_Force.com_Applications\" target=\"_blank\">good article on Salesforce blog<\/a> on various approaches suggested for pagination and one of them is using\u00a0StandardSetController.<\/p>\n<p style=\"text-align: justify;\">To keep article simple, I am posting complete Visualforce and Apex code with Comments inside. Account object is used as an example here.<!--more--><\/p>\n<figure id=\"attachment_3600\" aria-describedby=\"caption-attachment-3600\" style=\"width: 551px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/11\/Use-ListView-in-Visualforce-with-Paging-and-Navigation.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3600\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/11\/Use-ListView-in-Visualforce-with-Paging-and-Navigation.png?resize=551%2C433&#038;ssl=1\" alt=\"Use ListView in Visualforce with Paging and Navigation\" width=\"551\" height=\"433\" \/><\/a><figcaption id=\"caption-attachment-3600\" class=\"wp-caption-text\">Use ListView in Visualforce with Paging and Navigation<\/figcaption><\/figure>\n<p><strong>Apex class :<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n*  Description : Controller Class to show How to utilize existing List View in Apex with Pagination Support\r\n*\r\n*  Author : Jitendra Zaa\r\n*\/\r\npublic with sharing class ListViewDemo {\r\n\r\n  private String baseQuery = 'Select ID, Name FROM Account ORDER BY NAME ASC';\r\n  public String AccFilterId {get; set;}\r\n  private Integer pageSize = 10;\r\n\r\n  public ListViewDemo(){}\r\n\r\n  public ApexPages.StandardSetController AccSetController {\r\n        get{\r\n            if(AccSetController == null){\r\n                AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery));\r\n                AccSetController.setPageSize(pageSize);\r\n\r\n                \/\/ We have to set FilterId after Pagesize, else it will not work\r\n                if(AccFilterId != null)\r\n                {\r\n                  AccSetController.setFilterId(AccFilterId);\r\n                }\r\n            }\r\n            return AccSetController;\r\n        }set;\r\n    }\r\n\r\n  public ListViewDemo(ApexPages.StandardSetController c) {   }\r\n\r\n    \/\/Navigate to first Page\r\n    public void firstPage()\r\n    {\r\n      AccSetController.first();\r\n    }\r\n\r\n    \/\/Navigate to last Page\r\n    public void lastPage()\r\n    {\r\n      AccSetController.last();\r\n    }\r\n\r\n    \/\/Navigate to Next page\r\n    public void next()\r\n    {\r\n      if(AccSetController.getHasNext())\r\n      {\r\n        AccSetController.next();\r\n      }\r\n    }\r\n\r\n    \/\/Navigate to Prev Page\r\n    public void prev()\r\n    {\r\n      if(AccSetController.getHasPrevious())\r\n      {\r\n        AccSetController.previous();\r\n      }\r\n    }\r\n\r\n    public List&lt;Account&gt; getAccounts()\r\n    {\r\n      return (List&lt;Account&gt;)AccSetController.getRecords();\r\n    }\r\n\r\n    \/\/Get all available list view for Account\r\n    public SelectOption&#x5B;] getAccountExistingViews(){\r\n        return AccSetController.getListViewOptions();\r\n    }\r\n\r\n    \/**\r\n    * Reset List View\r\n    *\/\r\n    public PageReference resetFilter()\r\n    {\r\n      AccSetController = null;\r\n        AccSetController.setPageNumber(1);\r\n        return null;\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>Visualforce Page:<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page controller=&quot;ListViewDemo&quot;&gt;\r\nAvailable List Views for Account :\r\n  &lt;apex:form id=&quot;pageForm&quot;&gt;\r\n   \t   &lt;apex:selectList value=&quot;{!AccFilterId}&quot; size=&quot;1&quot; id=&quot;filterMenu&quot;&gt;\r\n     \t\t&lt;apex:selectOptions value=&quot;{!AccountExistingViews}&quot;&gt;&lt;\/apex:selectOptions&gt;\r\n     \t\t&lt;apex:actionSupport event=&quot;onchange&quot;  action=&quot;{!resetFilter}&quot; rerender=&quot;AccntTable&quot; status=&quot;ajaxStatus&quot;\/&gt;\r\n   \t   &lt;\/apex:selectList&gt;\r\n\r\n   \t   &lt;apex:actionStatus id=&quot;ajaxStatus&quot; startText=&quot;Loading...&quot;  stopText=&quot;&quot;\/&gt;\r\n\r\n   \t &lt;apex:pageBlock title=&quot;Accounts&quot;&gt;\r\n   \t    &lt;apex:pageBlockButtons &gt;\r\n                &lt;apex:commandButton action=&quot;{!firstPage}&quot; value=&quot;|&lt;&lt;&quot; reRender=&quot;AccntTable&quot;  status=&quot;ajaxStatus&quot; \/&gt;\r\n                &lt;apex:commandButton action=&quot;{!prev}&quot; value=&quot;&lt;&quot; reRender=&quot;AccntTable&quot;  status=&quot;ajaxStatus&quot; \/&gt;\r\n                &lt;apex:commandButton action=&quot;{!next}&quot; value=&quot;&gt;&quot; reRender=&quot;AccntTable&quot;  status=&quot;ajaxStatus&quot; \/&gt;\r\n                &lt;apex:commandButton action=&quot;{!lastPage}&quot; value=&quot;&gt;&gt;|&quot; reRender=&quot;AccntTable&quot;  status=&quot;ajaxStatus&quot; \/&gt;\r\n            &lt;\/apex:pageBlockButtons&gt;\r\n\r\n\t        &lt;apex:pageBlockTable value=&quot;{!Accounts}&quot; var=&quot;item&quot; id=&quot;AccntTable&quot;&gt;\r\n\t            &lt;apex:column value=&quot;{!item.name}&quot;\/&gt;\r\n\t        &lt;\/apex:pageBlockTable&gt;\r\n     &lt;\/apex:pageBlock&gt;\r\n   &lt;\/apex:form&gt;\r\n&lt;\/apex:page&gt;\r\n<\/pre>\n<p><strong>Test Class :<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@isTest\r\npublic class ListViewDemoTest\r\n{    \r\n    \r\n    @testSetup \r\n    static void createAccount() {\r\n        \/\/ Create common test accounts\r\n        List&lt;Account&gt; testAccts = new List&lt;Account&gt;();\r\n        for(Integer i=0;i&lt;20;i++) {\r\n            testAccts.add(new Account(Name = 'TestAcct'+i));\r\n        }\r\n        insert testAccts;        \r\n    }\r\n    \r\n     public static testMethod void getListView(){\r\n        \/\/Lets Assume we are writing Controller extension to use on List View of Account\r\n        List &lt;Account&gt; acctList = &#x5B;SELECT ID FROM Account];\r\n\t\t\r\n         \/\/Check Account created count by setup()\r\n         System.assertEquals(20,acctList.size());\r\n         \r\n        \/\/Start Test Context, It will reset all Governor limits\r\n        Test.startTest();\r\n\r\n        \/\/Inform Test Class to set current page as your Page where Extension is used\r\n        Test.setCurrentPage(Page.ListViewDemo);\r\n\r\n        \/\/Instantiate object of &quot;ApexPages.StandardSetController&quot; by passing array of records\r\n        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acctList);\r\n\r\n        \/\/Now, create Object of your Controller extension by passing object of standardSetController\r\n        ListViewDemo ext = new ListViewDemo(stdSetController);\r\n        \r\n        SelectOption&#x5B;] selOptions = ext.getAccountExistingViews();\r\n         \r\n        \/\/We should not assert count of list View as no control over creation of list view\r\n        \/\/but in my Dev org, I know count is 6 \r\n        System.assertEquals(6,selOptions.size());\r\n         \r\n         ext.firstPage();\r\n         List&lt;Account&gt; accFirsttPage = ext.getAccounts();\r\n         System.assertEquals( 10, accFirsttPage.size() );\r\n         \r\n         ext.next();\r\n         ext.prev();\r\n         ext.resetFilter();\r\n         ext.lastPage();\r\n          \r\n        \/\/Finish Test\r\n        Test.stopTest();\r\n     } \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are scenario in project lifecycle where developer creates SOQL or Dynamic SOQL to return expected result. Also if requirement changes they go back and change existing code to reflect updated SOQL. If you are good developer and avoid to change Apex code at most you will save your SOQL in &#8220;Custom Settings&#8221; and utilize [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,9,18],"tags":[337,84,140,152,331,185,190,211,336],"class_list":["post-3598","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","category-visualforce","tag-apex","tag-dynamic-soql","tag-listview","tag-pagination","tag-salesforce","tag-soql","tag-standardsetcontroller","tag-vf","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3278,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/visualforce\/using-fieldset-with-visualforce-and-apex\/","url_meta":{"origin":3598,"position":0},"title":"Using FieldSet with Visualforce and Apex","author":"Jitendra","date":"May 3, 2013","format":false,"excerpt":"One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its \"Maintenance\"\u009d, if New Filed is Added or needed to remove field we have to modify our code every time. However, Thanks to Salesforce that we have \"Field Set\"\u009d. With the\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Field Set in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":3598,"position":1},"title":"Salesforce Interview Questions \u2013 Part 11","author":"Jitendra","date":"August 19, 2012","format":false,"excerpt":"101. How to force lead assignment rule via Apex while updating or adding the Lead? Ans : To enforce Assignment Rules in Apex you will need to perform following steps: Instantiate the \"Database.DMLOptions\"\u009d class. Set the \"useDefaultRule\"\u009d property of \"assignmentRuleHeader\"\u009d to True. Finally call a native method on your Lead\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Pagination in SOQL using keyword Offset","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2835,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-apex-code-talk-important-questions-and-answers\/","url_meta":{"origin":3598,"position":2},"title":"Salesforce Apex Code talk &#8211; Important Questions and Answers","author":"Jitendra","date":"May 3, 2012","format":false,"excerpt":"Salesforce Apex Code talk on - 24-April-2012 , Important Interview Questions","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3331,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/how-to-prepare-for-dev-501-certification-salesforce\/","url_meta":{"origin":3598,"position":3},"title":"How to prepare for Dev 501 Certification &#8211; Salesforce","author":"Jitendra","date":"June 25, 2013","format":false,"excerpt":"This is first time i am going to write article on any Salesforce Certification. This time it is about \"How to Pass Salesforce Dev 501 Certification\". I have already received many request on how to prepare for this certification. Dev 501 certificate in Salesforce will validate that how comfortable you\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3325,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-visualforce-data-loader-and-soql-interview-question-part-17\/","url_meta":{"origin":3598,"position":4},"title":"Apex, Visualforce, Data Loader and SOQL Interview Question \u2013 Part 17","author":"Jitendra","date":"October 10, 2013","format":false,"excerpt":"161 : Sometimes while deleting record it gives error \"Object cannot be Deleted\". What is the reason for this kind of error ? Ans : This is generic error message prompted by Salesforce many times, which is not well informative. To get informative message, we can try to delete same\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":3598,"position":5},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=3598"}],"version-history":[{"count":3,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3598\/revisions"}],"predecessor-version":[{"id":4767,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3598\/revisions\/4767"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}