function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nish321Nish321 

Collection size 1085 exceeds maximum size of 1000

Could some please help me out regarding this?  It's throwing an errow when adding the bunch of records at a time.
 Below is my code:
public with sharing class AddMultipleRelatedProducts {
    
    public Project_Product__c CurrentPT {get;set;}
    
    public List<TaskTemplate> listTaskTemplate {get;set;}
     
    public string SearchKeyword {get;set;}
     
    public CustomIterable obj;
    
    public SET<ID> setSelectedProductId {get;set;}
    
    public List<Product_Code__c> listProductCode {get;set;}
    public List<Product_Code__c> listSelectedProductCode {get;set;}
    public boolean ShowProductCode {get;set;}
    Set<Id> productIds = new Set<Id>();
    Set<Id> SproductCodeIds = new Set<Id>();
    Set<Id> SelectedProductCodeIds = new Set<Id>();
    public List<Product__c> Products { get; set; }
      
    public AddMultipleRelatedProducts(){
        listSelectedProductCode = new List<Product_Code__c>();
        CurrentPT = new Project_Product__c();
        listTaskTemplate = new List<TaskTemplate>();
        setSelectedProductId = new SET<ID>();
        listProductCode = new List<Product_Code__c>();
        ShowProductCode = false;
        
        if(System.currentPageReference().getParameters().get('id') != null ){
            for(Project_Product__c pt : [Select Id From Project_Product__c Where Id =: System.currentPageReference().getParameters().get('id')]){
                CurrentPT = pt;
            }
        }   
         
        SelectedProductCodeIds = new Set<Id>();
        for(Products__c pj : [select Product_Code__c from Products__c where Project__c = :CurrentPT.Id order by Product_Code__c asc ])
        {
            SelectedProductCodeIds.add(pj.Product_Code__c);
        }
        
        productIds = new Set<Id>();
        for(Product_Code__c pc : [select Id, Product_Name__c from Product_Code__c  where Id in :SelectedProductCodeIds])
        {
            productIds.add(pc.Product_Name__c);
        }
        
        Products = [select Id, Name,  Division__c, Franchise__c,  Product_Code__c, Project__c from Product__c where Id in :ProductIds ]; // added Product_Code__c
        
        DoSearch();
    }
    
    public void DoSearch(){
        listTaskTemplate = new List<TaskTemplate>();
        
        string strQ = ' Select Id, Name ,Franchise__c,Division__c From Product__c Where Total_Product_Code__c > 0 ';

        string strKey = SearchKeyword + '%';
        
        if(SearchKeyword != null && SearchKeyword != ''){
            strKey = strKey.replace('*','');
            
            strQ = strQ + ' AND (Product_Code__c LIKE \'' + strKey + '\' OR Name Like \'' + strKey + '\'  OR Franchise__c Like \'' + strKey + '\' OR Division__c Like \'' + strKey + '\' ) ';
        }
        
        strQ = strQ + '  Order By Name ASC, Name ASC Limit 2000';
        
        obj = new CustomIterable(strQ); 
        obj.setPageSize = 100  ;
        next();
         
    }
    
    
    public pageReference DoCancel(){
        return new PageReference('/' + CurrentPT.Id);
    }
    
    public pageReference DoSelect() {
        pageReference pr = null;
        listSelectedProductCode = new List<Product_Code__c>();
        try{
            SET<ID> setTTId = new SET<ID>();
             
            for(TaskTemplate tt : obj.accInnerList){
                if(tt.IsSelected == true ){
                    setTTId.add(tt.TaskTemp.Id);
                }
            }
            
            if(setTTId.size() <= 0 ){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select at least one Product'));  
                return null;
            }
            
            List<Products__c> listRT = new List<Products__c>();
            
            for(TaskTemplate tt : obj.accInnerList){
                if(tt.IsSelected == true ){
                    setSelectedProductId.add(tt.TaskTemp.Id); 
                }
            }
            
            listProductCode = [Select Id, Name, Description__c, Product_Name__c,Product_Name__r.Name,Is_Selected__c FROM Product_Code__c WHERE Product_Name__c IN:setSelectedProductId AND Id NOT IN: SelectedProductCodeIds Order By Product_Name__r.Name ASC, Name ASC limit 2000 ];
            listSelectedProductCode = [ SELECT Id, Name, Description__c, Product_Name__r.Name, Product_Name__r.Franchise__c, Product_Name__r.Division__c, Product_Name__c FROM Product_Code__c WHERE Id IN: SelectedProductCodeIds order by Name ASC  ];
           // listSelectedProductCode = [ SELECT Id, Name, Description__c, Product_Name__r.Name, Product_Name__r.Franchise__c, Product_Name__r.Division__c, Product_Name__c FROM Product_Code__c WHERE Product_Name__c IN: productIds order by Name ASC  ];
            ShowProductCode = true; 
            // productIds
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));  
        }
        return pr;
        
    }
    
    
    public pageReference DoSave(){
        pageReference pr = null;
        try{
            SET<ID> setTTId = new SET<ID>();
             
            for(Product_Code__c tt : listProductCode){
                if(tt.Is_Selected__c == true ){
                    setTTId.add(tt.Id);
                }
            }
            
            if(setTTId.size() <= 0 ){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select at least one Product Code'));  
                return null;
            }
            
            List<Products__c> listRT = new List<Products__c>();
            
            for(Product_Code__c tt : listProductCode){
                if(tt.Is_Selected__c == true ){
                    Products__c rt = new Products__c();
                    
                    rt.Project__c = CurrentPT.Id;
                    rt.Product__c =  tt.Product_Name__c;                    
                    rt.Product_Code__c = tt.Id;
                    listRT.add(rt);
                }
            }
            
            if(listRT.size() > 0){
                upsert listRT;
            }

            pr = new PageReference('/' + CurrentPT.Id);
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));  
        }
        return pr;
        
    }
    
    
    public Boolean hasNext {
        get {
            return obj.hasNext();
        }
        set;
    }
    
    public Boolean hasPrevious {
        get {
            return obj.hasPrevious();
        }
        set;
    }
    
    public void next() {
        listTaskTemplate = obj.next();
    }
    
    public void previous() {
        listTaskTemplate = obj.previous();
    }
    
    
}

 
@Karanraj@Karanraj
Collection variable in apex controller class can hold upto the size of 1000.
If you want to store more than 1000 then you can use collection into collection List<List<string>>

Check this blog post for the sample - http://www.tgerm.com/2011/01/visualforce-apex-repeat-10000-items.html
Check this forum post - https://developer.salesforce.com/forums/?id=906F000000096eLIAQ