You need to sign in to do that
Don't have an account?

why is this logic skipping records?
i am trying to move multiple records between two lists. when i use something like this it works only if i move one record at a time. for example if i try to move 5, it only moves 3, and leaves 2 behind.
what am i doing wrong?
if(availableProducts!=null && availableProducts.size() != 0 ) { for(Integer i=0; i< availableProducts.size(); i++) { if(availableProducts.get(i).selected==true) { availableProducts.Remove(i); } } }
public List<productWrapper> availableProducts {get; set;} {availableProducts = new List<productWrapper>();}
public class productWrapper { public integer index {get; set;} public product2 prod {get; set;} public boolean selected {get; set;} public SFDC_520_QuoteLine__c quoteLine {get; set;} { quoteLine = new SFDC_520_QuoteLine__c(Sales_Discount__c = 0.0); } }
An excerpt from Apex Guide which talks about best practices while looping through collections
Iterating Collections
Collections can consist of lists, sets, or maps. Modifying a collection's elements while iterating through that collection is not
supported and causes an error. Do not directly add or remove elements while iterating through the collection that includes
them.
Adding Elements During Iteration
To add elements while iterating a list, set or map, keep the new elements in a temporary list, set, or map and add them to the
original after you finish iterating the collection.
Removing Elements During Iteration
To remove elements while iterating a list, create a new list, then copy the elements you wish to keep. Alternatively, add the
elements you wish to remove to a temporary list and remove them after you finish iterating the collection.
hi hisrinu,
thank you i have read that and actually thats the only way i can get it to work. this is what i started with...
the maps are used to make sure records are not doubled up.
let me know if you see any inefficiencies here.
Hi Walter,
I encountered same problem before, and hence I used while loop instead of for loop and it is consitent enough. Following code might suit your needs
In this code, everytime a loop iteration starts, the size of list is evaluated and executed accordingly.
hope this helps in building effecient code.
Hi Sarge,
Thank you . that is a great example for me to learn from. I appriciate it.