You need to sign in to do that
Don't have an account?
louisa 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
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
The problem with the code is on this line below. You create a new SelectOption object so it gets a new identifier each time. You probably want to use a Set<Id> or Set<String> in this case to check for duplicate. See the example below:
All Answers
The problem with the code is on this line below. You create a new SelectOption object so it gets a new identifier each time. You probably want to use a Set<Id> or Set<String> in this case to check for duplicate. See the example below:
I thought is would be related to creating new instances but wasn't sure how to get around it.
It working perfectly!