[SalesForce] Visualforce Error: Collection Size Exceeds Maximum

I have a VF page that uses a custom controller and I get the following error:

Visualforce Error: Collection size 4,820 exceeds maximum size of 1,000.

The controller was working properly, but I added an IF/ELSE clause (lines 18 – 36). I assume that's causing the issue, but I'm not sure what I need to do to fix. Can anyone help?

Controller

public class VF_SFController{

public List<E_S_F__c> ESF {get; set;}

    private final Opportunity opp;
    public VF_SFController(ApexPages.StandardController myController){
        ESF = new List<E_S_F__c>();
            if (!Test.isRunningTest())
            {
            myController.addFields(new List<String>{'Id', 'website_s__c', 'MT_Site_URLs__c', 'OwnerId', 'Owner.Phone', 'RecordType.Name'});
            }
        opp=(Opportunity)myController.getrecord();
    }

    public E_S_F__c ESF2 = new E_S_F__c();
        public void EngSF(){

        IF(opp.RecordType.Name == 'Eng Mid'){

            ESF2.Opportunity__c = opp.Id;
            ESF2.Websites__c = opp.MT_Site_URLs__c;
            IF(opp.MT_Site_URLs__c != null){
                ESF2.Website_Complete__c = TRUE;
                }
            ESF2.Sales_Representative__c = opp.OwnerId;

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE isPrimary = TRUE)
                            FROM Opportunity
                            WHERE id = :opp.id];
            ESF2.Partner_Contact__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;
        }
        Else{

            ESF2.Opportunity__c = opp.Id;
            ESF2.Websites__c = opp.website_s__c;
            IF(opp.website_s__c != null){
                ESF2.Website_Complete__c = TRUE;
                }
            ESF2.Sales_Representative__c = opp.OwnerId;

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Signatory')
                            FROM Opportunity
                            WHERE id = :opp.id];
            ESF2.Partner_Contact__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;

        IF(opp.Region__c.startsWith('AA')){
            Opportunity o2 = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Billing')
                            FROM Opportunity
                            WHERE id = :opp.id];
            ESF2.Billing_Contact__c = o2.opportunityContactRoles.size() != 0 
                                    ? o2.opportunityContactRoles[0].contactId  
                                    : null;
        }
        }

            ESF.add(ESF2);
        }


    public PageReference save() {

        insert ESF;

       PageReference RetPage = new PageReference('/apex/VF_SF_View?id=' + ESF[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }
}

Best Answer

I don't think if else is causing the issue.

Any vf component that iterates through records / list , can only show 1000 records in one go and hence you are getting errors.

What you can do is

  1. Add readonly="true" on the page level, this will increase the limit to 10k but you won't able to do any DML.
  2. You can use Remoting to bring the data , and use something like Jquery / AngularJs to render table, I did similar blog http://blogforce9.blogspot.com/2014/10/showing-more-than-1k-records-using-pbe.html and http://blogforce9.blogspot.in/2015/04/angular-js-sobject-remote-part-1.html
  3. Alternatively you can either try to limit the query or use pagination / offset