0

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; 
    }
}
5
  • The description seems quite clear...I assume you loop through your OpportunityContactRoles on the page?
    – Adrian Larson
    Commented Sep 26, 2016 at 18:09
  • Adrian -- On my page, I pull the Primary and/or Billing contact role into a custom field. I don't think I have a loop. Commented Sep 26, 2016 at 19:30
  • You are displaying a collection...any of <apex:repeat>, <apex:pageBlockTable>, <apex:dataTable>, etc would be considered looping through the results.
    – Adrian Larson
    Commented Sep 26, 2016 at 19:34
  • What comprises the 4K records? From the code it appears you are working off a single opp. Can there really be 4K contact roles? It would seem that there is code missing and the error is not caused by what you have in the question. I just do not see a possible way of getting 4K records with no loop unless the page is causing the list to be populated many times without clearing
    – Eric
    Commented Sep 26, 2016 at 22:01
  • Thanks. I was confused as well because my VF page is only displaying 1 record at a time. Turns out this error was being caused by a package a developer installed. Commented Sep 27, 2016 at 15:28

3 Answers 3

1

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
1
  • Thanks. I was confused as well because my VF page is only displaying 1 record at a time. Turns out this error was being caused by a package a developer installed. Commented Sep 27, 2016 at 15:28
0

Thanks for all the feedback. Apparently there was a package installed that was causing this error. It had nothing to do with the code I posted, although the error was being generated as a result of going to the VF page. Thank you for all the potential solutions, and sorry for the wild goose chase!

-1

Error isnt because of any conditional blocks. In VF , the collection can store upto 1000 items. which is exceeding in this present scenario.

As a workaround below steps can be undertaken:

1.Put a limit on your SOQL

  • Not Recomended as the result wont be contianing all the records returned by the SOQL query

2.Use pagination

  • The Pagination will allow you to show only the controlled amount of records at a time

3.or Use @readonly annotation on method "EngSF"

  • This will render the method as readonly and all DML operation are forbidden. Due to this property you can fetch and store more records in collections.
1
  • Thanks. I was confused as well because my VF page is only displaying 1 record at a time. Turns out this error was being caused by a package a developer installed. Commented Sep 27, 2016 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .