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
louisa barrett 7louisa barrett 7 

Distinct values in selectOption

Hi,
I was wondering if someone could help me. I am trying to populate a List of selectOption with distinct values.
I thought by using a set I could achieve this, but I'm still getting duplicates.

By code is as follows:

public List<selectOption> getAvailableFilters(){
        List<selectOption> options = new List<SelectOption>();
        List<Opportunity> availableOpps = new List<Opportunity>();
        availableOpps = [SELECT Account.Name, Name, Total_Opportunity_Amount__c, StageName FROM Opportunity WHERE IsWon = False AND Account.Parent.id=: Parent_Acc_Id];
         options.add(new SelectOption('All','All'));
        FOR(Opportunity opp:availableOpps)
        {
            List<opportunity> tempOptionList = new List<opportunity>();
            Set<selectOption> mySetOfOptions = new Set<selectOption>();
            tempOptionList.add(opp);
            for(opportunity myOpp:tempOptionList){
                if (mySetOfOptions.add(new SelectOption(myOpp.Account.ID, myOpp.Account.Name))){
                    options.add(new SelectOption(myOpp.Account.ID, myOpp.Account.Name));
                }
            }
                  }
       
        return options;
    }


Any help would be much appreciated.
Thanks
Best Answer chosen by louisa barrett 7
RaidanRaidan
Hi Louisa,

The problem with the code is on this line below. You create a new SelectOption object so it gets a new identifier each time. 
mySetOfOptions.add(new SelectOption(myOpp.Account.ID, myOpp.Account.Name))
You probably want to use a Set<Id> or Set<String> in this case to check for duplicate. See the example below:
List<SelectOption> options = new List<SelectOption>();
Set<Id> accountIdSet = new Set<Id>();
for (Opportunity opp:availableOpps) {
    if (!accountIdSet.contains(opp.AccountId)) {   //Id not in Set, add to options
        options.add(new SelectOption(opp.AccountId, opp.Account.Name));
        accountIdSet.add(opp.AccountId);
    }
}


 

All Answers

RaidanRaidan
Hi Louisa,

The problem with the code is on this line below. You create a new SelectOption object so it gets a new identifier each time. 
mySetOfOptions.add(new SelectOption(myOpp.Account.ID, myOpp.Account.Name))
You probably want to use a Set<Id> or Set<String> in this case to check for duplicate. See the example below:
List<SelectOption> options = new List<SelectOption>();
Set<Id> accountIdSet = new Set<Id>();
for (Opportunity opp:availableOpps) {
    if (!accountIdSet.contains(opp.AccountId)) {   //Id not in Set, add to options
        options.add(new SelectOption(opp.AccountId, opp.Account.Name));
        accountIdSet.add(opp.AccountId);
    }
}


 
This was selected as the best answer
louisa barrett 7louisa barrett 7
Thank you very much.
I thought is would be related to creating new instances but wasn't sure how to get around it.
It working perfectly!