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
vishwa attikerivishwa attikeri 

Problem in PageReference


Can we use PageReference inside for loop,


i am trying to pass the id using for loop but it gives error  as Too many script statments 200001

This is my code

public pageReference downloaddata()
	 Pagereference pp;
	 integer j = selectedAttachments.size();
	 for(Integer i=0; i<=j; j++)
	  pp=new Pagereference('/servlet/servlet.FileDownload?file='+selectedAttachments[i].id);
	 //return pp;
	 return pp;


<apex:commandbutton action="{!downloaddata}" value="Download" />


Here im trying to download the attachment files that is more than one file


please help me how to solve this issue

how i can achieve this





The issue is that your loop is larger than the documented execution limits.


Currently, salesforce only allows 200,000 executed code statements.


I recommend that you use some WHERE or LIMIT clauses in your SOQL statment to stay below the limit or otherwise consider a batch apex class.

vishwa attikerivishwa attikeri

Hi colemab,


thanks for your Replay


i have used where clause and i this list contains only four records


i have one question

can we use 'PageReference pp=new pageReference();

inside the for loop to display new multiple new window at one click,


and how can i solve above problem, here i want to download selected Attachments at one click





You might want to do a system.debug on your variable J because it looks like it is larger than 200,000.


As far as I know, you can't use page reference to open mutiple new windows at once.  I could be wrong but I think each browser window is only allowed to have one page reference displayed at a time.


I think you will need to use java script to call a web service on the salesforce plat form once for each of your files.