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
tdeptdep 

Combine Query Selectlist multiple items into 1 value

Hi,

 

I currently have a for loop inside a Selectlist to gather items within that field. Is there a way to combine multiple values that are the same? So only 1 value shows in the dropdown rather than 2? Is there a way to truncate multiple values into 1?

 

Example: Change Management is on 2 records in the system but I only want to show 1 on the drop-down/selectList.

 

 

Here is the SelectList I am using

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();

options.add(new selectOption('', '- None -'));

for (Object r : [select FIELDNAME from Object])
{
options.add(new selectOption(r.FIELDNAME, r.FIELDNAME));
}

return options;
}

 

Thanks in advance


Best Answer chosen by Admin (Salesforce Developers) 
tdeptdep

Got it!

 

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();

options.add(new selectOption('', '- None -'));
Set<String> teststring = new Set<String>();
for (Object r : [select FIELDNAME from Object])
{
IF(!teststring.contains(r.FIELDNAME)) 
{
t
eststring.add(r.FIELDNAME);
options.add(new selectOption(r.FIELDNAME, r.FIELDNAME));
} } return options; }

All Answers

tdeptdep

Is there a way to add an IF statement within the FOR loop to check against the values added? and then do not execute the .add if the value is present?

 

If so, I am unsure on how to run that test against values that were added. I think this may be the right approach.

tdeptdep

Got it!

 

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();

options.add(new selectOption('', '- None -'));
Set<String> teststring = new Set<String>();
for (Object r : [select FIELDNAME from Object])
{
IF(!teststring.contains(r.FIELDNAME)) 
{
t
eststring.add(r.FIELDNAME);
options.add(new selectOption(r.FIELDNAME, r.FIELDNAME));
} } return options; }
This was selected as the best answer