+ Start a Discussion
ShaTShaT 

System.LimitException: Too many script statements: 200001

Hi ,

 i m displaying Products in my viusal force page . I have 3000 products it is giving me following error -:

System.LimitException: Too many script statements: 200001

 

code -

soql ='Select p.Suggested_Price__c, p.Style__c, p.Spec_Sheet__c, p.Size__c ,p. name from Product2 ';

Map<id,Product2> mapAcc = new Map<id,Product2>((List<Product2>)Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 50000'));
            productList=mapAcc.values();
            system.debug('--------productList----------'+productList);
            total_no_of_Records=productList.size();
            if(productList.size()>0){
                total_no_of_pages = productList.size()/noOfRecordPerPage;                 
                if(math.mod(productList.size(),noOfRecordPerPage) > 0){
                   total_no_of_pages = total_no_of_pages +1;
                }
                  for(integer i = 0; i<total_no_of_pages ; i++){
                    integer counter = i+1;
                     for(integer j = pageStartValue ; j< pageEndValue; j++){
                        try{
                            if(productList.size()>j)
                            prolist2.add(productList[j]);
                            
                            for(Product2 p :prolist2){
                                OppProducts pr = new OppProducts(p);                
                                pr.IsSelected = false;
                                listProducts.put(p.id,pr);
                            }
                            mapProducts.put(counter,listProducts.values());
                           
                        }
                        catch(Exception e) {
                        }
                }
                pageStartValue = pageEndValue;
                pageEndValue = noOfRecordPerPage*(i+2);            
                }
                lstProducts=mapProducts.get(selectedPage);
            }

 

I trying to get all products and putting them in a map with size of 25 .So  how can i refine my code so i dont get above error .

 

Thanks

Shailu

sfcksfck

Not sure what exactly is causing it, but even if we fix it it will break again eventually as you add more and more products.

 

Rather than getting all the products out of the database and then dividing them up into pages, it is better to get 25 products out each time.

 

Standard set controller has pagination built in  - that might be worth a try

 

Otherwise you could use the Offset keyword to get the right set of 25 out each time ...

James LoghryJames Loghry

Why construct a map of pages / list of products?

 

I'd leave that up to your pagination.

 

E.g. if the user clicks on a next page link, then that returns the 25 products pertaining to the current page.

 

That way, instead of a loop doing a 50,000 * x scripts in your set up,  the pagination would do a 25 * x scripts when the user requests it.

 

Make sense?