Collection size exceeds maximum size of 1000 error

It 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;
        }
       
    }
}


Cheers!!!

5 comments:

  1. Thank you so much for sharing this.. This is a time saver. Happy coding!

    ReplyDelete
  2. Its good one. Thanks for sharing it.

    ReplyDelete
  3. Thanks very much for posting this. I noticed one thing that I think is missing. The only time tmp is added to thousandblocks is when it reaches the limit which results in the final tmp list not being added. So if there were 1020 then the last 20 would not get added.

    So right before the return thousandBlocks; statement I added the line
    thousandBlocks.add(new limitWrapper(tmpcase,loopCount));

    ReplyDelete
  4. This solution saved me today! Thanks a lot! :)

    ReplyDelete