Collection size exceeds maximum size of 1000 error

Collection size exceeds maximum size of 1000 error

Collection size exceeds maximum size of 1000 error in Salesforce means List, Set & Map size exceeded more than 1000 to be shown on the Visualforce page.

To avoid this, use wrapper class and get set of first 1000 records, save it and then retrieve the next and so on.

Sample code:

Visualforce page:

<apex:page controller="thousandLimit">   
   <apex:pageBlock >
      <apex:repeat value="{!thousandBlocks}" var="block">
            <apex:pageBlockTable value="{!block.cases}" var="c">
            <apex:column value="{!c.CaseNumber}"/>
            <apex:column value="{!c.owner.name}"/>
            <apex:column value="{!c.App_Process__c}"/>
            <apex:column value="{!c.Load__c}"/>
            <apex:column value="{!c.subject}"/>
            <apex:column value="{!c.Estimated_Completion_Date__c}"/>  
            <apex:column value="{!c.Complete__c}"/>
            <apex:column value="{!c.Priority}"/>
            <apex:column value="{!c.Case_Age_Days__c}"/>                   
            </apex:pageBlockTable>
        </apex:repeat>
     </apex:pageBlock>
</apex:page>

Apex Controller:

public class thousandLimit
{
    private limitWrapper[] thousandBlocks = new limitWrapper[]{};
   
    private final integer listLimit;
   
    public thousandLimit()
    {
        listLimit = 1000;
    }
   
    public limitWrapper[] getthousandBlocks()
    {
        thousandBlocks = new limitWrapper[]{};
       
        integer counter = 0;
        integer loopCount = 0;
        case[] tmpcase = new case[]{};
       
        for( Case c : [ SELECT CaseNumber, owner.name, App_Process__c, Load__c, subject, Estimated_Completion_Date__c, Complete__c, Priority, Case_Age_Days__c
            FROM Case
            WHERE (Status != 'Closed' or Status != 'Declined') and Estimated_Completion_Date__c = null and owner.Alias like 'EA%' order by owner.name]
        )
        {
            if(counter < listLimit)
            {
                tmpcase.add(c);
                counter++;
            }
            else
            {
                loopCount++;
                thousandBlocks.add(new limitWrapper(tmpcase,loopCount));
                tmpcase = new case[]{};
                tmpcase.add(c);
                counter = 0;
            }           
        }
       
        if(thousandBlocks.size() == 0)
        {
            loopCount++;
            thousandBlocks.add(new limitWrapper(tmpcase,loopCount));
        }
       
        return thousandBlocks;
    }
   
    public class limitWrapper
    {
        public case [] cases {get;set;}
        public integer blockNumber {get;set;}
        public limitWrapper(case[] accs, integer i)
        {
            cases = accs;
            blockNumber = i;
        }
       
    }
}

Leave a Reply