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
Hrishikesh KolheHrishikesh Kolhe 

System.FinalException: Cannot modify a collection while it is being iterated.I am getting below error in apex code with visualforce page can anyone help

public class invoice {



public Salesorder__c so{get;set;}
public Invoice__c inc{get;set;}
public list<Salesorder_Line_Product__c> solp{get;set;}
public list<Invoice_Line_Product_No__c> inclp{get;set;}
public string rid{get;set;}


public invoice (){
rid=apexpages.currentpage().getparameters().get('id');
so= new Salesorder__c();
inc= new Invoice__c();
solp=new list<Salesorder_Line_Product__c>();
inclp= new list<Invoice_Line_Product_No__c>();

so=[Select id,Account_Management__c,Contact_Management__c,Delivery_Lead_Time__c,Opportunity_Management__c,Payment_Terms__c,Quote__c,Remarks__c,Salesorder_Date__c,Warranty__c,Valid_Till__c from Salesorder__c where id=:rid];
inc.Salesorder__c=so.id;
inc.Account_Management__c=so.Account_Management__c;
inc.Contact_Management__c=so.Contact_Management__c;
inc.Delivery_Lead_Time__c=so.Delivery_Lead_Time__c;
inc.Opportunity_Management__c=so.Opportunity_Management__c;
inc.Payment_Terms__c=so.Payment_Terms__c;

inc.Quote__c=so.Quote__c;

// inc.Remarks__c=so.Remarks__c;

inc.Warranty__c=so.Warranty__c;
inc.Valid_Till__c=so.Valid_Till__c;
//inc.Total_Amount__c =so.Total_Amount__c ;



//inc.Invoice_Date__c=so.Salesorder_Date__c;


solp=new list<Salesorder_Line_Product__c>();
inclp= new list<Invoice_Line_Product_No__c>();


solp=[Select id,Account_Name__c,Actual_Price__c,Unit_price__c,Total_Price__c,Product_Description__c,CGST__c,CGST_Amount__c,IGST__c,IGST_Amount__c,
      SGST__c,SGST_Amount__c,Quantity__c,Opportunity_Name__c,Quote_Name__c,Salesorder_Name__c,Discounted_Price__c,Discount_Type__c,Product__c from Salesorder_Line_Product__c where Salesorder_Name__c=:rid ];

 for (Salesorder_Line_Product__c  solipo:solp)
 {

Salesorder_Line_Product__c sl =new Salesorder_Line_Product__c ();

 sl.Salesorder_Name__c=solipo.Salesorder_Name__c;
sl.Account_Name__c=solipo.Account_Name__c;

sl.Actual_Price__c=solipo.Actual_Price__c;

sl.Unit_Price__c=solipo.Unit_Price__c;
sl.Quantity__c=solipo.Quantity__c;


//sl.Total_Price__c=solipo.Total_Price__c;

sl.Product_Description__c=solipo.Product_Description__c;


sl.CGST__c=solipo.CGST__c;
//sl.CGST_Amount__c=solipo.CGST_Amount__c;

sl.IGST__c=solipo.IGST__c;

//sl.IGST_Amount__c=solipo.IGST_Amount__c;

sl.SGST__c=solipo.SGST__c;

//sl.SGST_Amount__c=solipo.SGST_Amount__c;
sl.Opportunity_Name__c=solipo.Opportunity_Name__c;

sl.Quote_Name__c=solipo.Quote_Name__c;
sl.Product__c=solipo.Product__c;

//sl.Discounted_Price__c=solipo.Discounted_Price__c;

sl.Discount_Type__c=solipo.Discount_Type__c;

// sl.Discount_Value__c = solipo.Discount_Value__c ;



solp.add(sl);


}
update solp;
 }

public pagereference save(){
 insert inc;
 solp=new list<Salesorder_Line_Product__c>();

 for(Salesorder_Line_Product__c solipo : solp){

Salesorder_Line_Product__c sl =new Salesorder_Line_Product__c ();
//solipo .Salesorder_Name__c=inc.id;
solp.add(sl);
}
insert solp;
pagereference pr=new pagereference('/'+rid);
return pr;

}
}

Invoice get generated but getting error after adding products
System.FinalException: Cannot modify a collection while it is being iterated
SwethaSwetha (Salesforce Developers) 
HI Hrishikesh,
As the error message suggests, you cannot add items to or remove items from a collection that is feeding a loop.

See similar scenarios from past
https://salesforce.stackexchange.com/questions/368030/how-to-solve-fatal-error-system-finalexception-cannot-modify-a-collection-whi
https://salesforce.stackexchange.com/questions/325221/i-am-getting-this-error-system-finalexception-cannot-modify-a-collection-while
https://salesforce.stackexchange.com/questions/31042/trigger-error-cannot-modify-a-collection-while-it-is-being-iterated

Thanks