11

How do i get the complete list of records in a list view. I know using getRecords() we can get but its only limited to maximum of 20 records. But i need list of complete records for the List view. Is it possible?.

ApexPages.StandardSetController setCon;
        List<Bug__c> lChanges = new List<Bug__c>();
        public GenerateCardListView_Cntrl(ApexPages.StandardSetController controller) {
            setCon = controller;
            lChanges = setCon.getRecords();            
            system.debug('&&&&&'+setCon.getRecords());
            system.debug('&&&&&'+setCon.getRecords().size());
        }

The size in the debug returns 20 if the size is more than 20.

7

The StandardSetController works a bit like a database cursor, you need to ask for records a page at a time. The key methods are getHasNext() and next(). Using these around each call to the getRecords() method until you've moved through the entire record set. This simple controller demonstrates this...

public with sharing class DemoStandardSetController 
{
    public List<Test__c> AllRecords {get;set;}

    public DemoStandardSetController(ApexPages.StandardSetController stdSetController)
    {
        stdSetController.addFields(new List<String> { 'Name' });

        AllRecords = new List<Test__c>();

        while(stdSetController.getHasNext())
        {
            for(Test__c testRecord : (List<Test__c>) stdSetController.getRecords())
            {
                AllRecords.add(testRecord);
            }
            stdSetController.next();
        }
    }
}

My demo page looks like this...

<apex:page standardController="Test__c" extensions="DemoStandardSetController" recordSetVar="fred">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!AllRecords}" var="Record">
            <apex:column value="{!Record.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

enter image description here

| improve this answer | |
  • 1
    It seems that with your code, the records in last page won't be gotten. When we arrive in the last page, the getHasNext() returns false and we don't go to the while. – SF_user Jan 22 '15 at 14:00
6

Use getResultSize() method of the StandardSetController. The getRecords() returns only the list of objects in the current page set. And this is set by default to 20 records.

Here is the documentation. And here are all methods of the StandardSetController.

ApexPages.StandardSetController setCon;
        List<Bug__c> lChanges = new List<Bug__c>();
        public GenerateCardListView_Cntrl(ApexPages.StandardSetController controller) {
            setCon = controller;
            lChanges = setCon.getRecords();       

            // Here is the output of the set size
            system.debug('&&&&&'+ setCon.getResultSize());    

            system.debug('&&&&&'+setCon.getRecords());
            system.debug('&&&&&'+setCon.getRecords().size());
        }
| improve this answer | |
2

In order to get the last values you should repeat the line in the while still one time.

 while(stdSetController.getHasNext())
 {
        for(Test__c testRecord : (List<Test__c>) stdSetController.getRecords())
        {
            AllRecords.add(testRecord);
        }
        stdSetController.next();
 }
 for(Test__c testRecord : (List<Test__c>) stdSetController.getRecords())
 {
        AllRecords.add(testRecord);
 }
| improve this answer | |
0

Set setPageSize(integer) for StandardSetController,

e.g.

ApexPages.StandardSetController setCon;
        List<Bug__c> lChanges = new List<Bug__c>();
        public GenerateCardListView_Cntrl(ApexPages.StandardSetController controller) {
            setCon = controller;
            **setCon.setPageSize(100);**
            lChanges = setCon.getRecords();            
            system.debug('&&&&&'+setCon.getRecords());
            system.debug('&&&&&'+setCon.getRecords().size());
        }
| improve this answer | |
0

getRecords() only returns the records displayed in the page.

If you want getRecords() to return all the records in the view option query, trick your APEX code to temporarily display all records, then reset your controller set to display the initial number of records.

This approach is faster and more performant than using nested for/while loops.

e.g.

   ApexPages.StandardSetController setCon;

    List<Bug__c> lChanges = new List<Bug__c>();
    public GenerateCardListView_Cntrl(ApexPages.StandardSetController controller) {
         setCon = controller;
         integer selectedPageSize = setCon.getPageSize();
         setCon.setPageSize(2000);//Trick: increase number of records displayed
         lChanges = setCon.getRecords(); //if they are less than 2000, all records should be returned
         setCon.setPageSize(selectedPageSize);//Display initial number of records
     }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.