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

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; }
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)
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?
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.
Hi kyle.t ,
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.