+ Start a Discussion
Navneeth RajNavneeth Raj 

Illegal assignment from List<System.SelectOption> to Set<String> ERROR

Illegal assignment from List<System.SelectOption> to Set<String>  
@Line 15

public class FieldDependencyPickList {
public    Map<String,List<String>> cityMap{get;set;}
    public List<SelectOption> place {set;get;}
    public List<SelectOption> city {set;get;}
    public String selCity    {get;set;}
    public String selPlace    {set;get;}

public FieldDependencyPickList(){
    cityMap=new Map<String,List<String>>();
    List<String> hcityPlaces=new List<String> {'apet','npet','bpet','abpet'};
        List<String> apcityPlaces=new List<String> {'rjy','kkd','tni','vskp'};
            cityMap.put('HYD',hcityPlaces);            
            cityMap.put('AP',apcityPlaces);
                Set<String> place=cityMap.keySet();
L15   place=new List<SelectOption>();
        city=new List<SelectOption>();
        SelectOption so=new SelectOption('none','-None-');
        city.add(so);
        place.add(so);
    for(String p:place){
        SelectOption nsp=new SelectOption(p,p);
        city.add(nsp);
}  
    }
        public void placesGetter(){    
            place.clear();
            List<String> sp=cityMap.get(selcity);
            for(String pp:sp){
                SelectOption so1=new SelectOption(pp,pp);
                place.add(so1);
    }
  }
}
Amit Chaudhary 8Amit Chaudhary 8
Please remove below code and try

  for(String p:place){
        SelectOption nsp=new SelectOption(p,p);
        city.add(nsp);
}

Please let us know if that will help u​
Navneeth RajNavneeth Raj
Why to remove that code?
Amit Chaudhary 8Amit Chaudhary 8
Because of below reason
1) place is not String so that should bel
for( SelectOption so: place)
{
 city.add(so:);
}

2) No need to add same value again in city because you already added the same value in last to last line
Navneeth RajNavneeth Raj
Scenario here is If i seect the country from the country PickList then the related places in the country should be available
EXAMPLE: COUNTRY PickList contains these options (None,India,America,China) & CAPITAL PickList contains New Delhi, Washington & Beijing. OK
Navneeth RajNavneeth Raj
Ok, Amit. I recoded it as below

public class FieldDependencyPickList {
public    Map<String,List<String>> cityMap{get;set;}
    public List<SelectOption> place {set;get;}
    public List<SelectOption> city {set;get;}
    public String selCity    {get;set;}
    public String selPlace    {set;get;}

public FieldDependencyPickList(){
    cityMap=new Map<String,List<String>>();
    List<String> hcityPlaces=new List<String> {'apet','npet','bpet','abpet'};
        List<String> apcityPlaces=new List<String> {'rjy','kkd','tni','vskp'};
            cityMap.put('HYD',hcityPlaces);            
            cityMap.put('AP',apcityPlaces);
                Set<String> plc=cityMap.keySet();
        place=new List<SelectOption>();
        city=new List<SelectOption>();
        SelectOption so=new SelectOption('none','-None-');
        city.add(so);
        place.add(so);
    for(String p:plc){
        SelectOption nsp=new SelectOption(p,p);
        city.add(nsp);
}  
    }
        public void placesGetter(){    
            place.clear();
            List<String> sp=cityMap.get(selcity);
            for(String pp:sp){
                SelectOption so1=new SelectOption(pp,pp);
                place.add(so1);
    }
  }
}

Now, here comes another error saying "duplicate value found: <unknown> duplicates value on record with id: <unknown>" at LINE 0
VineetzVineetz
You have declared place as set in line 14. Please correct that.
VineetzVineetz
Why dont you directly take it in a set instead of having a list and then moving it to set ?
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.  i hope that will help u
<apex:page controller="sample">
   
    <apex:form >
   
    <apex:pageBlock>
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem>
                <apex:outputLabel value="State"/>
            </apex:pageblockSectionItem>       
            <apex:pageblockSectionItem>               
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
                </apex:selectList>               
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem>
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>           
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>           
        </apex:pageBlockSection>       
    </apex:pageBlock>

    </apex:form>

</apex:page>
 
public class sample
{
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));       
        options.add(new SelectOption('India','India'));
        options.add(new SelectOption('America','America'));
        return options;
    }
   
    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'India')
        {      
            options.add(new SelectOption('NDL','New Delhi'));
            options.add(new SelectOption('UP','UP'));
        }
        else if(state == 'America')
        {      
            options.add(new SelectOption('NYK','New York'));
            options.add(new SelectOption('deloit','deloit'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }     
        return options;
    }      
}

Please let us know if this will help you

Thanks,
Amit Chaudhary
RAVITEJA C 19RAVITEJA C 19
Hi 
Iam facing one problem of my code getting error
Error:
(Illegal assignment from List<System.SelectOption> to List<selectoption> at line 13 column 28)

visualforce code:
-----------------------
<apex:page controller="simplepicklist">

<apex:form >

    <apex:pageBlock >

        <apex:pageBlockSection >

            <apex:outputLabel >State</apex:outputLabel>

            <apex:pageblockSectionItem >               

                <apex:selectList size="1" value="{!state}">

                    <apex:selectOptions value="{!states}"/>

                    <apex:actionSupport event="onchange" reRender="a"/>

                </apex:selectList>               

            </apex:pageblockSectionItem>

            <apex:outputLabel value="City"/>      

            <apex:pageblockSectionItem >

                <apex:selectList size="1" value="{!city}" id="a">

                    <apex:selectOptions value="{!cities}"/>

                </apex:selectList>

            </apex:pageblockSectionItem>     

        </apex:pageBlockSection>

    </apex:pageBlock>

</apex:form>

</apex:page>

Apex code:
--------------
public with sharing class simplepicklist {

 

    public String state { get; set; }

    public String city { get; set; }

     

    public List<SelectOption> getStates() {

        List<SelectOption> stOptions = new List<SelectOption>();

        stOptions.add(new SelectOption('None','--None--'));

        stOptions.add(new SelectOption('AP','Andhra Pradesh'));

        stOptions.add(new SelectOption('TN','Tamil Nadu'));

        stOptions.add(new SelectOption('KA','Karnataka'));

        return stOptions;

    }

 

    public List<SelectOption> getCities() {

        List<SelectOption> ciOptions = new List<SelectOption>();

        if(state=='AP'){

            ciOptions.add(new SelectOption('VSKP','Vishakapatnam'));

            ciOptions.add(new SelectOption('BZA','Vijayawada'));

            ciOptions.add(new SelectOption('TPTY','Tirupati'));

        }

        else if(state=='TN'){

            ciOptions.add(new SelectOption('CHE','Chennai'));

            ciOptions.add(new SelectOption('CBE','Coimbatore'));

        }

        else if(state=='KA'){

            ciOptions.add(new SelectOption('SBC','Bangalore'));

            ciOptions.add(new SelectOption('MAQ','Mangalore'));

        }

        else if(state=='None'){

            ciOptions.add(new SelectOption('None','--None--'));

        }

        return ciOptions;

    }

 

}

if anyone help and resolve this issue
Regards
Raviteja