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

How to remove duplicates in a picklist using code?
Hi ,
I haave a requirement when i populate 3 picklist hierarchical using code. Its working fine but i get duplicates.
Example : xyz
xyz
abc
My code to populate one of the picklist is given below. This gets the elements from a field in the object and i get the value from it by iterating. My issue is i need to eliminate duplicates.
public List<selectOption> getLanguageList() {
List<selectOption> options = new List<selectOption>();
for (List<NKJ_EmailTemp_Classify__c> listMapping : [select Language__c
from NKJ_EmailTemp_Classify__c
where Certificate_Brand__c = :selectedBrand])
{
for (NKJ_EmailTemp_Classify__c obj: listMapping) {
options.add(new selectOption(obj.Language__c, obj.Language__c));
}
}
system.debug(options);
return options ;
}
Note >> Blue are my custom objects where i select and in the innerloop i iterate over the fields to generate by options list.
public List<selectOption> getLanguageList() {
List<selectOption> options = new List<selectOption>();
Set<string>languages = new set<string>();
for (NKJ_EmailTemp_Classify__c listMapping : [select Language__c
from NKJ_EmailTemp_Classify__c
where Certificate_Brand__c = :selectedBrand])
{
if(!languages.contains(listMapping.Language__c))
{
options.add(new selectOption(listMapping.Language__c, listMapping.Language__c));
}
}
system.debug(options);
return options ;
}
All Answers
list<NKJ_EmailTemp_Classify__c> listMapping = new list<NKJ_EmailTemp_Classify__c>() ;
List<selectOption> options = new List<selectOption>();
for (NKJ_EmailTemp_Classify__c lm : [select Language__c
from NKJ_EmailTemp_Classify__c])
{
listMapping .add(lm);
}
for (NKJ_EmailTemp_Classify__c obj: listMapping) {
options.add(new selectOption(obj.id, obj.Language__c));
}
}
system.debug(options);
return options ;
}
Create a Set variable and then assign values into that and read it.
Set is basically unique ordere value list.
Thanks Swati i tried that approach you listed but didnt work. Thanks for your effort . Appeciate it.
Hi,
Thanks fo rthe suggestion. I tried with SET but look slike it has some compatability issue with selectOptions.
if you write it this way it does not work
SET<selectOption> options = new SET<selectOption>();
Probably Map might help . Will get back after trying.
Nyjil
public List<selectOption> getLanguageList() {
List<selectOption> options = new List<selectOption>();
Set<string>languages = new set<string>();
for (NKJ_EmailTemp_Classify__c listMapping : [select Language__c
from NKJ_EmailTemp_Classify__c
where Certificate_Brand__c = :selectedBrand])
{
if(!languages.contains(listMapping.Language__c))
{
options.add(new selectOption(listMapping.Language__c, listMapping.Language__c));
}
}
system.debug(options);
return options ;
}
Hi Duncan,
Your code set me off in the right direction. However a minor change is need to code as we need to add to the SET. modified code below. My issue is resolved. Thanks a lot guys my POC demo will now go on smooth ;)
for (NKJ_EmailTemp_Classify__c listMapping : [select Language__c
from NKJ_EmailTemp_Classify__c
where Certificate_Brand__c = :selectedBrand])
{
if(languages.contains(listMapping.Language__c))
{
// dont add duplicate, do nothing
system.debug('DUP PRESENT');
}
else {
languages.add(listMapping.Language__c);
system.debug('DUP NOT PRESENT');
options.add(new selectOption(listMapping.Language__c, listMapping.Language__c));
}
}
system.debug(options);
return options ;
}