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
divya manohardivya manohar 

default values in picklist

Hi
New to salesforce and looking to consolidate concepts.
when I select city name picklist the places picklist displays the values but it doesnt display the default value.
I want to display the default value in place picklist when I select the a city name from city picklist.
public class PickListExample4 
{
  public Map<String,List<String>> cityMap{get;set;}
  
  public List<SelectOption> cities{get;set;}
  public List<SelectOption> places{get;set;}
  
  public string selected{get;set;}

  public Pagereference Display()
  {
     cityMap=new Map<String,List<String>>();
     cities=new List<SelectOption>();
     places=new List<SelectOption>();
     
     List<String>  hydplaces=new List<String>{'SRNagar','LBNagar'};
     List<String>  banglplaces=new List<String>{'Electronics City','Malleswaram'};
     
     cityMap.put('Hyderabad',hydplaces);
     cityMap.put('Bangalore',banglplaces);
          
     SelectOption n=new SelectOption('none','--None--');
     cities.add(n);
     places.add(n);

     Set<string> keys=cityMap.Keyset();
    
     for(String s:keys)
     {
            SelectOption op1=new SelectOption(s,s);
            cities.add(op1); //Bind cities to picklist.
     }
     return null;
  }
    
  public void getData()
  {
    if (selected != 'none')
    {
       places.clear();
       
       //create places picklist
       SelectOption n=new SelectOption('none','--None--');
       places.add(n);
       
       List<String> myplaces=cityMap.get(selected);
       system.debug('==>myplaces'+myplaces); //this will fetch the places for the selected city
       
       //Now bind the places to picklist
       for(string s:myplaces)
       {
          SelectOption op=new SelectOption(s,s);
          places.add(op); //Bind cities to picklist.
       }
    }
  }
}
<apex:page controller="PickListExample4"
           action="{!Display}">

<apex:form >
<apex:pageBlock title="Field dependency">

<apex:pageBlockSection >

<apex:pageBlockSectionItem >
   <apex:outputLabel value="Cities"/>
   <apex:selectList size="1" 
                     value="{!selected}">
                  
  <apex:selectOptions value="{!cities}"/>
  <apex:actionSupport event="onchange"
                      reRender="one"
                      action="{!getData}"/>

</apex:selectList>
</apex:pageBlockSectionItem>

<apex:pageBlocksectionItem>
                    <apex:outputLabel value="Places"/>
                    <apex:selectList  size="1" 
                                      id="one">
                    <apex:selectOptions value="{!places}" />
                    </apex:selectList>
 </apex:pageBlocksectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
divya manohar
Arun Deepan LJArun Deepan LJ
Hi Divya Manohar,

This can be done in APEX by setting the value in your variable you are using in Select Option. In your case you should create a new APEX String variable in your class: For Example

public String selectedCountry
{
   get
    {
       if(selectedCountry==null)
           selectedCountry='CANADA';
    }
set;
}
public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));
                options.add(new SelectOption('CANADA','Canada'));
                options.add(new SelectOption('MEXICO','Mexico'));
                return options;
        }

<apex:selectList size="1" id="countryList" onChange="setCountry(this.value);" value="{!selectedCountry}">
    <apex:selectOptions value="{!items}"/>
    </apex:selectList>

 
AshlekhAshlekh
Hi,

You can just assign  selectedCountry='CANADA' value in constructor.

-Thanks
Ashlekh Gera