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
Walter@AdicioWalter@Adicio 

please advise, moving multiple records from one list to another

is this OK or is there a better way to go about moving multiple records from one list to another?

 

the goal is for the user to click checkboxes for items in alist and click one 'add' / 'remove' button to move records back and forth.

 

 

	public PageReference massRemoveSelectedItems() 
 	{

    	list<wrap> remove = new list<wrap>();
    	list<wrap> retain = new list<wrap>();
    	
    	
		if(selectedList!=null && selectedList.size() != 0 )
		{
	    	for(wrap w : selectedList)
	    	{
	    		if(w.selected==true){
	    			remove.add(w);
	    		}
	    		
	    		if(w.selected==false)
	    		{
	    			retain.add(w);
	    		}
	    	}
		}
    	
  		if(retain != null && retain.size() != 0 ){
	    	selectedList.clear();
	    	
	    	for(wrap w : retain)
	    	{
	    		selectedList.add(w);
	    	}

  		}
  		
  		if(remove!= null && remove.size() != 0 ){

	    	for(wrap w : remove)
	    	{
	    		availableToSelectList.add(w);
	    	}
 		}
    	
    	return null;
    }

 

kyle.tkyle.t

It seems like you are using more steps than necessary.  From what I can glean from the code you have two lists: AvailableToSelectList & SelectedList.

 

I am assuming they are initially populated outside of this method. 

 

It would seem that you would be able to just run through each list and process each record as necessary:

(this is untested, but should give you a general idea)

 

public PageReference massRemoveSelectedItems() {
if(selectedList!=null && selectedList.size() != 0 ){
        for(Integer i=0; i< SelectedList.size(); i++){
            if(SelectedList[i].selected==false)
            {
                availableToSelectList.add(SelectedList[i]);
                selectedList.Remove(i);
            }
        }
    }
    
    if(availableToSelectList!=null && availableToSelectList.size() != 0 ){
        for(Integer i=0; i< availableToSelectList.size(); i++){
            if(availableToSelectList[i].selected==false)
            {
                selectedList.add(electedList[i]);
                availableToSelectList.Remove([i]);
            }
        }
    }
}
Walter@AdicioWalter@Adicio

 

hi kyle.t,

 

thank you for that. it makes sense and seems much more efficient. i have a question. as far as i can tell its not working correctly however. i notice when i try and move a lot of records at once for example from "available" to "selected", its leaving some behind.

 

i cant really see a pattern, for example if i select 5 to move from "available", it only moves 3 to "selected", and it leaves the other 2 as -checked- in "available". if i click again with the 2 it left behind it moves 1 more to "selected" and leaves the last one behind in "available" again as -checked-.

 

any ideas?

 

 

    public pageReference getSelected()
    {
		if(selectedProducts!=null && selectedProducts.size() != 0 )
		{        
			for(Integer i=0; i< selectedProducts.size(); i++)
			{           
				if(selectedProducts[i].selected==true)            
				{    
					selectedProducts[i].selected=false;            
					availableProducts.add(selectedProducts[i]);                
    	  		 	selectedProducts.Remove(i);            
				}        
			}    
		}        
    	  	
		if(availableProducts!=null && availableProducts.size() != 0 )
		{        
			for(Integer i=0; i< availableProducts.size(); i++)
			{            
				if(availableProducts[i].selected==true)            
				{               
	  				 availableProducts[i].selected=false;
	  				 selectedProducts.add(availableProducts[i]);                
	  				 availableProducts.Remove(i);           
				}        
			}    
		}
    	  		
  		return null;
    }
    

 

 

 

kyle.tkyle.t

Walter,

   I believe I know what is happening... As we cycle through the list, we are also removing objects from that list.  Assume we have three items in the list: A,B,C which have index 0,1,2.

 

So our list looks something like this (index, item):  {0,A; 1,B; 2,C} Now when we start the for loop, i=0.  if the item at index 0 is moved to the other list and then removed from the original list, then everything else moves up one place so we end up with {0,B; 1,C}.  now i = 1, but if B was selected we overlook it.  I have updated the code below to have 2 loops for each list.  The first check if the item should move and the second removes it.  Hope this works.

 

public pageReference getSelected()
{
	if(selectedProducts!=null && selectedProducts.size() != 0 )
	{        
		for(Integer i=0; i< selectedProducts.size(); i++)
		{
			if(selectedProducts[i].selected==true)
			{
				availableProducts.add(selectedProducts[i]);
			}
		}
		
		for(Integer i=0; i< selectedProducts.size(); i++)
		{           
			if(selectedProducts[i].selected==true)            
			{    
				selectedProducts[i].selected=false;                 
				selectedProducts.Remove(i);            
			}        
		}    
	}        
		
	if(availableProducts!=null && availableProducts.size() != 0 )
	{        
		for(Integer i=0; i< availableProducts.size(); i++)
		{            
			if(availableProducts[i].selected==true)            
			{               
				selectedProducts.add(availableProducts[i]);
			}
		}
		
		for(Integer i=0; i< availableProducts.size(); i++)
		{            
			if(availableProducts[i].selected==true)            
			{               
				 availableProducts[i].selected=false;           
				 availableProducts.Remove(i);           
			}        
		}    
	}
			
	return null;
}
    

 

Walter@AdicioWalter@Adicio

Hi ,

 

I cant get it to stop skipping records that way. i can only get it to work when i move items to a temporary list, clear the list then move them back where i want them.

 

thanks for your help.