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
Christopher PezzaChristopher Pezza 

Convert a deduped List to a Select Options in apex

Ok so I am trying to create a Select List in Visualforce of a list that was just deDuped with this code below:

public with sharing class CustomerHealth{

Set<String> cmSet = new Set<String>();

public CustomerHealth() {
    for(Customer_Health__c ch : [SELECT Customer_Success_Manager__c FROM Customer_Health__c                                                     WHERE Customer_Success_Manager__c <> ' ']){
             cmSet.add(ch.Customer_Success_Manager__c);
       }//END for
   }//END public 

public List<SelectOption> getCSMItems() {
        List<SelectOption> csmoptions= new List<SelectOption>();
        Decimal i;
        for (i=0; i < cmSet.size(); i++) {
            options.add(new SelectOption(cmSet[i], cmSet[i]));
        }
        return csmoptions;
    }


}//END public with sharing

When I do this the public List<SelectOption> won't let me save i get this Error: "Expression must be a list type: SET<String>" The line is: "options.add" line
 
This is what im trying to output it in in visualforce:
<apex:selectList size="1" styleClass="btn btn-green btn-xs" value="{!CSMFilter}" onchange="RefreshTable1()">
   <apex:selectOption value="{!cmSet}"></apex:selectOption>
</apex:selectList>
Now i have and actiofunction tied to it but its not relevant, This is before i tried adding the for loop. which i did a get of the deduped list to call that list and output it but i get this error when i do that: "Invalid selectOptions found. Use SelectOption type in Apex."

Help please :)
 

Best Answer chosen by Christopher Pezza
Gopal RathoreGopal Rathore
Hi Christopher Pezza,
We Can't Use Set as array So We have to use List instead of Set or We have to itrate set values.
i.e,
Controller----
public with sharing class CustomerHealth {

Public String CSMFilter {get ; set;}

public Set<String> cmSet {get; set;}

public CustomerHealth() {

       cmSet = new Set<String>();

    for(Customer_Health__c ch : [SELECT Customer_Success_Manager__c FROM Customer_Health__c   WHERE Customer_Success_Manager__c <> ' ']){
             cmSet.add(ch.Customer_Success_Manager__c);
       }//END for
   }//END public

public List<SelectOption> getCSMItems() {
        List<SelectOption> csmoptions= new List<SelectOption>();
        for (String s : cmSet) {
           
            csmoptions.add(new SelectOption(s, s));
        }
        return csmoptions;
    }
}



Vf Page---

<apex:page controller="CustomerHealth" >
<apex:form >
  <apex:selectList size="1" styleClass="btn btn-green btn-xs" value="{!CSMFilter}" onchange="RefreshTable1()">
  <apex:SelectOptions value="{!csmItems}"/>
</apex:selectList>
</apex:form>
</apex:page>





Change Select Options Value With csmItems

Regards
Gopal Rathore

All Answers

Gopal RathoreGopal Rathore
Hi Christopher Pezza,
We Can't Use Set as array So We have to use List instead of Set or We have to itrate set values.
i.e,
Controller----
public with sharing class CustomerHealth {

Public String CSMFilter {get ; set;}

public Set<String> cmSet {get; set;}

public CustomerHealth() {

       cmSet = new Set<String>();

    for(Customer_Health__c ch : [SELECT Customer_Success_Manager__c FROM Customer_Health__c   WHERE Customer_Success_Manager__c <> ' ']){
             cmSet.add(ch.Customer_Success_Manager__c);
       }//END for
   }//END public

public List<SelectOption> getCSMItems() {
        List<SelectOption> csmoptions= new List<SelectOption>();
        for (String s : cmSet) {
           
            csmoptions.add(new SelectOption(s, s));
        }
        return csmoptions;
    }
}



Vf Page---

<apex:page controller="CustomerHealth" >
<apex:form >
  <apex:selectList size="1" styleClass="btn btn-green btn-xs" value="{!CSMFilter}" onchange="RefreshTable1()">
  <apex:SelectOptions value="{!csmItems}"/>
</apex:selectList>
</apex:form>
</apex:page>





Change Select Options Value With csmItems

Regards
Gopal Rathore
This was selected as the best answer
Christopher PezzaChristopher Pezza
Worked Perfectly  Thanks!!!