Why can Visualforce iterate over >1000 item collection if it’s a map?

As per Visualforce Limits:

Maximum number of collection items that can be iterated in an
iteration component such as <apex:pageBlockTable> and <apex:repeat> — 1,000

So normally 1,000 is the limit. If try to iterate over more than 1,000 items then we receive this error:

collection size exceeds maximum size error

I observe when we use List<Sobject> variable to iterate over apex:repeat
then we get this(collection size exceeds maximum size) error. But when I use Map<Id, Sobject> then I can iterate over more than 10,000 records (I tested with 20,000 records).

Noted: You can only iterate over 20,000 if view state does not exceed 135 KB

My Conclusion: It looks like this limitation doesn’t apply on Map<Id, Sobject>.

I tried to find documentation related to this issue, but could find nothing. It doesn’t seem like Salesforce’s official documentation mentions it anywhere.

So what is the reason map doesn’t hit the limit? Looks like a Salesforce hidden secret, or I am just imagining things?

Answer

The original question asked how many records can be iterated, to which my answer is below.

It seems that for all practical purposes, you are only limited by Viewstate/CPU/Heap when taking this approach. Here is an extreme example where I was able to iterate a collection of one million items with no error. There is no output, and there is hardly any data. Even under these conditions, I ran out of CPU Time before running into any sort of Visualforce specific Collection Size error.

Sample Controller

public with sharing class MegaMap
{
    public Map<Integer, String> data { get; private set; }
    public MegaMap()
    {
        data = new Map<Integer, String>();
        for (Integer i = 0; i < 1000000; i++)
            data.put(i, '');
    }
}

Sample Page

<apex:page controller="MegaMap">
    <apex:repeat value="{!data}" var="i" />
</apex:page>

I ran through this demo at the end and added <apex:outputText value="{!i} /> inside the <apex:repeat> tag. Still worked.

Attribution
Source : Link , Question Author : Ratan Paul , Answer Author : Adrian Larson

Leave a Comment