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

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++)



 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
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.


    public PageReference getSelected()
		list<productWrapper> move = new list<productWrapper>();
        list<productWrapper> keep = new list<productWrapper>();
        for(productWrapper w : availableProducts)
	       if(w.selected == true)
	       if(w.selected == false)
		for(productWrapper w : keep)
		for(productWrapper w : move)

        return null;



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


List<ProductWrapper> selectedProducts = new List<ProductWrapper>();
if(availableProducts != null && availableProducts.size() != 0){
    integer currentItem = 0;
    while(availableProducts.size() > currentItem){     
        if(availableProducts[currentItem].selected == true){
             when you remove an item, items below move up by one index.
             Issuing remove method will remove the list item and the method will return
             the item removed. This returned item can be used to enlist
             the 'selectedProducts' list.
             //since we are not removing any item, we have to proceed to next item

 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.