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
Michael J. LupinoMichael J. Lupino 

Deduplicate list of records for insert into SelectList (dropdown)

Hi, 

I have some functionality that reads a group of records from a related list for display in a pageblock table based on a criteria i have set in the SOQL query. I have some values in a field lets call the field filterA that could be duplicate of other items. I'd like to read the full list of records for my query and dedup any entries. 

I've tried set but as the record ids are unique, I'll end up with the same number of records being read. Any ideas on how I can screen out the duplicates? 

Here's a visual example: 

Filter A (values)
A
A
A
A
A
A
B
B
C

My end result would be to get a list with A,B,C vs. displaying all values as I plan to pass A,B,C, to a selectlist and selectoption. I'm interested in this because This field Filter A would then be used in another piece of functionality to effectively filter based on a action from a command button and a SOQL query that would engage Filter A. I prefer to build this list based on the data vs. hardcoding. 
Best Answer chosen by Michael J. Lupino
Anupama SamantroyAnupama Samantroy
Hi Michael,

Seems like your field FilterA is a non groupable field. Some fields like picklist, multipicklists are non groupable. 
If the list you want to create is list of strings then you can try Set<String>.
 
Set<String> setFilterValues = new Set<String>();
for(Object__c obj: select filterA from object where parentId=:currentrecordid){
setFilterValues.add(obj.filterA);
}

What is the datatype of the field Filter? Is it lookup to some other object?


Thanks
Anupama

All Answers

Anupama SamantroyAnupama Samantroy
Hi Michael,

Try to use the aggregate query to group the records by the filterA.

Thanks
Anupama
Michael J. LupinoMichael J. Lupino
Hi, I have a where cause to grab a parentid from the list im pulling. I pulled an error stating that i could not do that with a where clause. Thanks for your reply and help, Michael
Anupama SamantroyAnupama Samantroy
HI Michael,

Can you post your SOQL query here. I can look at it.

Thanks
Anupama
Michael J. LupinoMichael J. Lupino
It went something like this: Select filtera from object where parentid = :currentrecordid groupby filtera The error said the function was not supported in aggregate query
Anupama SamantroyAnupama Samantroy
Hi Michael,

Seems like your field FilterA is a non groupable field. Some fields like picklist, multipicklists are non groupable. 
If the list you want to create is list of strings then you can try Set<String>.
 
Set<String> setFilterValues = new Set<String>();
for(Object__c obj: select filterA from object where parentId=:currentrecordid){
setFilterValues.add(obj.filterA);
}

What is the datatype of the field Filter? Is it lookup to some other object?


Thanks
Anupama
This was selected as the best answer
Michael J. LupinoMichael J. Lupino
FilterA Its a formula field that uses another lookup on the object to retrieve a string of text. It is then displayed on the screen and used as values in my dropdown list.
Anupama SamantroyAnupama Samantroy
Hi Michael,

In that case you can use the code snippet in my previous comment to create the list of select option and show it as picklist on page. As formula fields are non groupable.

Thanks
Anupama
Michael J. LupinoMichael J. Lupino
Thanks everyone for your post. I was able to solve by using a set. I needed a list of <SelectOptions> for my dropdown. I used a set of strings (NameofSet) to grab descrete values. I then iterated the set into the list which is returned on the page. Here's an example: 

    for(objectname__c r : [Select id, valuetoadd From objectname where fieldname_c ='filter goes here']){
             NameofSet .add(r.valuetoadd__c);
       }//END for

    for (String a : NameofSet) {
           
            nameoflist.add(new SelectOption(a, a));
        }
        return nameoflist;
        } //returns back to dropdown list on vf page