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
VinslikeuVinslikeu 

How to create dependent fields according to picklist value selection

I'm new on salesforce.

I'm trying to create dependent fields according to picklist value selection.

In the following code I want:

1: when I select "None" in picklist no field should display.

2: if I select 1st option in the picklist; only field1 should get display.

3: if I select 2nd option in the picklist; only field2 should get display.

3: and if I select 3rd option in the picklist; all the fields1,2&3 should get display.

 

Please Help me out of this task.. Thanks!

 

/////////////////////Visualforce///////////////////////

<apex:page controller="FieldSelection">
      <apex:pageBlock title="fields">
          <apex:form >
                <b>State:</b> <t/><t/>
                <apex:selectList id="sta" value="{!SelectedState}" size="1">
                    <apex:selectOptions value="{!StateList}" />
                </apex:selectList>
                <br/><br/>
                <b>Field1:</b><t/><t/>
                <apex:inputText required="true" id="city1" value="{!city1}" />
                <br/><br/>
                <b>Field2:</b><t/><t/>
                <apex:inputText required="true" id="city2" value="{!city2}" />
                <br/><br/>
                <b>Field3:</b><t/><t/>
                <apex:inputText required="true" id="city3" value="{!city3}" />
                
          </apex:form>
      </apex:pageBlock>
</apex:page>

 

 

 

/////////////////////////Controller/////////////////////////////////

public with sharing class FieldSelection {

    public String city3 { get; set; }

    public String city2 { get; set; }

    public String city1 { get; set; }

    public String SelectedState { get; set; }
    
    public list<SelectOption> StateList{
    get{
            list<SelectOption> st= new list<SelectOption>();
            st.add(new SelectOption('','- None -'));
            List<UV_Account__c> lFINAL = New List<UV_Account__c>();
            list<UV_Account__c> cc=[select State__c from UV_Account__c];
            Set<String> sNames = New Set<String>();
            for (UV_Account__c c : cc){
            if (sNames.Contains(c.State__c) == FALSE){
               sNames.add(c.State__c);
               lFINAL.add(c);
        }
        }
            
            for( UV_Account__c fc : lFINAL)
            {
                st.add(new SelectOption(fc.id,fc.State__c));
            }
            return st;
       }
     set; }

}

zmzigazmziga

I am trying to do the same thing and I tried three things, none of them worked.

First option: I created rerender option on fields I'd like to show/hide. In those fields I created rendered="{!IF(picklist='something',true,false)}" ...Does not work.

Second option: I created Boolean which is set to false by default and when I changed selection in first field, I would call method that changes values of that Boolean. In other field I'd just create rendered="{!thisBoolean}"....Does not work.

Third option was to create ajax function which hides/shows...does not work.

 

So basically I don't know what else to do :S

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try below code.

 

<apex:page controller="CustomSettings_controller" >
<apex:pageBlock >
<apex:form >
<apex:actionFunction name="rerenderStates" rerender="ListState" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>

<apex:pageblocksection >
Country <apex:selectList id="ListCountry" value="{!country}" size="1" onchange="rerenderStates(this.value)">
<apex:selectOptions value="{!countrylist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>

<apex:pageblocksection >
State <apex:selectList id="ListState" value="{!states}" size="1">
<apex:selectOptions value="{!statelist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>

</apex:form>
</apex:pageBlock>
</apex:page>

 

 

public class CustomSettings_controller {


Map<String, countrystate__c> c = countrystate__c.getall();


public list<selectoption> getStateList() {
list<selectoption> s = new list<selectoption> ();
s.add(new selectoption('','Select one'));
if(country==null){
country='';
}
else
if(c.containskey(country)){
s.clear();
string sm = c.get(country).States__c;
for(string st: sm.split(';'))
s.add(new selectoption(st,st));
}

return s;
}


public List<SelectOption> getCountrylist() {
List<SelectOption> country = new List<SelectOption>();
country.add(new selectoption('India','---SELECT ONE---'));
list<string> countryname= new list<string>();
countryname.addall(c.keyset());

for(string s : countryname){
country.add(new selectoption(s,s));

}

return country;
}


public String states { get; set; }

public String country{ get; set;}

}

Regards,

Rajesh.

VinslikeuVinslikeu

Hi Rajesh!

Thanks for your countribution.

The code you have mention is seems to create a dependent picklist values..

but i want to create dependent fields according to picklist value selection.

 

consider we have 3 profiles. Zonal Manager, Reginal Manager and Area Manager.

if select Zonal Manager in the picklist then the fields related to that profile should get display.

may be that fields contains picklist or simple text fields.

accordingly same for the rest of the options selection in the picklist.

 

Thanks!

Rajesh SriramuluRajesh Sriramulu

Hi,

 

U need to fetch the picklist values from database means above is correct.

OR
If u want to define picklist values in controller means below one is correct:

 

<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>


Apex Code:

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('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        return options;
    } 
    
    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'TN')
        {       
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state == 'KL')
        {       
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('MVL','Mavelikara'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }       
}

 

Regards,

Rajesh.

VinslikeuVinslikeu

I tried this code.

Still not get appropriate output

on changing pickvalue selection fields doesnot changes..

 

 

<apex:page controller="FieldSelection">
<apex:pageBlock title="fields">
<apex:form >
<b>State:</b> <t/><t/>
<apex:selectList id="sta" value="{!SelectedState}" size="1">
<apex:selectOptions value="{!StateList}" />
</apex:selectList>
<br/><br/>
<b>Field1:</b><t/><t/>
<apex:inputText required="true" id="city1" value="{!city1}" rendered="{!or(If(SelectedState!= Goa, true, false),(IF(SelectedState== '', true, false)))}" />
<br/><br/>
<b>Field2:</b><t/><t/>
<apex:inputText required="true" id="city2" value="{!city2}" rendered="{!IF(SelectedState!= 'goa', true, false)}"/>
<br/><br/>
<b>Field3:</b><t/><t/>
<apex:inputText required="true" id="city3" value="{!city3}" rendered="{!if(SelectedState!='Maharashtra',true, false)} "/>

</apex:form>
</apex:pageBlock>
</apex:page>