0

When the user clicks on the checkbox I'm adding that particular item i the selectedContacts list the below code works but the problem with that it clears everytime, if i comment the clear line then its duplicating the records.

So what is the best way to avoid duplicate?

List<Contact> selectedContacts = new List<Contact>(); 

Public PageReference Selected()
{
    selectedContacts.clear();
    for(accountwrapper  accwrapper : accountList)
    {
         for(ContactWrapper conwrapper : accwrapper.contactset)
         {                
            if(conwrapper.isSelected == true)
            {
                selectedContacts.add(conwrapper.oContact );
            }
         }              
    }        
    return null;
}


public List<Contact> getSelectedContacts() 
{
    if(selectedContacts.size()>0)
    { 
        return selectedContacts;
    } 
    else
    { 
        return null;
    } 
}    
2
  • 2
    Simply change the List to a set? Also, why would there be duplicate in the list presented to the user to select, can you get that list down to no duplicates?
    – Eric
    Commented Jul 20, 2015 at 13:53
  • working on a treeview so the user select the checkbox to select all the records or any particular record and can user also select different parent record to select so I like to have non-duplication records in the selectedContracts hope this make sense
    – Nick
    Commented Jul 20, 2015 at 13:57

3 Answers 3

3

I completely misunderstood your question and posted answer regarding .clear() method.

You should use Set<Contact> as suggested by other answers and comments.

But you cannot use this Set<Contact> directly in value attribute in VF <apex:repeat>, <apex:pageBlockTable> and <apex:dataTable> because these tags expect List in value attribute.

So you can rewrite your Selected() method like below

Public PageReference Selected()
{
    Set<Contact> setSelectedContacts = new Set<Contact>();
    setSelectedContacts.addAll(selectedContacts);
    for(accountwrapper  accwrapper : accountList)
    {
        for(ContactWrapper conwrapper : accwrapper.contactset)
        {                
            if(conwrapper.isSelected == true)
            {
                setSelectedContacts.add(conwrapper.oContact );
            }
        }              
    } 
    selectedContacts = new List<Contact>();
    selectedContacts.addAll(setSelectedContacts);
    return null;
}

Hope it helps.

1
  • still overriding the previous selected item, the point is to keep the previous selected plus any new item select
    – Nick
    Commented Jul 20, 2015 at 14:03
2

I have ran into this issue before. I completely avoid the .clear() method now as it seems to clear all previous instances of the list also. To avoid duplicates, see if you can figure out a way to due this with a set. Sets only contain unique values

1

You can use .......

Set<Contact> selectedContacts = new Set<Contact>(); 

instead of

List<Contact> selectedContacts = new List<Contact>(); 
6
  • Compile Error: Return value must be of type: List<Contact> public List<Contact> getSelectedContacts() getting error
    – Nick
    Commented Jul 20, 2015 at 14:08
  • change ----- public List<Contact> getSelectedContacts() --to-- public Set<Contact> getSelectedContacts() Commented Jul 20, 2015 at 14:12
  • okay after i have changed, now i'm getting this error Unknown property 'SetValue.size'
    – Nick
    Commented Jul 20, 2015 at 14:15
  • you didn't mentioned "SetValue.size" anywhere in your, ....... please check if you are trying to getting size of some collection use setValue.size(); Commented Jul 20, 2015 at 14:22
  • I don't even know where is that coming from but if you look at my question I posted the second method where it's checking the size
    – Nick
    Commented Jul 20, 2015 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .