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
lavanya gottumukkalalavanya gottumukkala 

Rendering values in a picklist field based on value selected in another picklist field in a visualforce page

Hi All,

We have below requirement.
We have a visualforce page and in that page, we use 2 picklist fields(country,Taxtype).Now we need to render the picklist values of 'Taxtype' based on value selected in the 'country' picklist field in that visualforce page.Can any one help me on this matter?

Thanks in advance
hitesh90hitesh90
Hello lavanya,

Please find below sample code for your requirement.

let me know if you have any question on this.

Visualforce Page:
<apex:page standardController="Contact" extensions="extContactpageController">    
    <apex:form >
        <apex:pageBlock title="Contact">
            <apex:pageblockButtons>
                <apex:commandButton value="Save" action="{!saveContact}"> </apex:commandButton>      
                <apex:commandButton value="Cancel" action="{!cancelRecord}"> </apex:commandButton>  
            </apex:pageblockButtons>
            <apex:pageBlockSection >
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="First Name"></apex:outputLabel>
                    <apex:inputField value="{!cont.FirstName}"/>
                </apex:pageblockSectionItem>  
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="Last Name"></apex:outputLabel>
                    <apex:inputField value="{!cont.LastName}"/>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem>                
                    <apex:outputLabel >Country</apex:outputLabel>
                    <apex:actionRegion immediate="true" >
                        <apex:selectList value="{!country}" size="1" title="Country" label="Country"> 
                            <apex:selectOptions value="{!CountryItems}"/> 
                            <apex:actionsupport event="onchange" reRender="state" >
                            </apex:actionsupport>                    
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="State"></apex:outputLabel>
                    <apex:selectList value="{!state}" id="state" size="1" title="State" label="State"> 
                        <apex:selectOptions value="{!StateItems}"/> 
                    </apex:selectList>
                </apex:pageblockSectionItem>                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
public class extContactpageController{

public String country {get;set;}
    public String state {get;set;}
    public Contact cont{get;set;}
    
    public extContactpageController(ApexPages.StandardController controller) {
          cont = new contact();
    }

    public pagereference saveContact(){       
        cont.lastname = cont.lastname ;
        cont.firstName =cont.firstName;
        cont.Couuntry__c = country ;
        cont.State__c = state;
        insert cont;
        PageReference ref= new ApexPages.StandardController(cont).view();        
        return ref;
    }
    
    public void cancelRecord(){
        cont.lastname = '';
        cont.firstName ='';
        Pagereference page = new pagereference('/003/o');
        page.setredirect(true);
    }
    
    public List<SelectOption> getCountryItems(){
        List<SelectOption> options = new List<SelectOption>(); 
            options.add(new SelectOption('','-- None --')); 
            options.add(new SelectOption('US','US')); 
            options.add(new SelectOption('UK','UK')); 
            options.add(new SelectOption('INDIA','INDIA'));
            return options;
    }
    
    public List<SelectOption> getStateItems(){
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('','-- None --')); 
        if(country == 'US'){            
            options.add(new SelectOption('US1','US1')); 
            options.add(new SelectOption('US2','US2')); 
            options.add(new SelectOption('US3','US3'));
            return options;
        }
        if(country == 'India'){
            options.add(new SelectOption('India1','India1')); 
            options.add(new SelectOption('India2','India2')); 
            options.add(new SelectOption('India3','India3'));
            return options;
        }
        if(country == 'UK'){
            options.add(new SelectOption('uk1','uk1')); 
            options.add(new SelectOption('uk2','uk2')); 
            options.add(new SelectOption('uk3','uk3'));
            return options;
         }
        return options;
    }  
}

Let me know if you have any question on this.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
lavanya gottumukkalalavanya gottumukkala
Hi thanks for your quick response
I would like to dynamically render the state values without hardcoding them in the code
hitesh90hitesh90
Hi lavanya,

Yes you can add dynamic options there instead of hard code.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
s shekar 20s shekar 20
Hi Hitesh,How are you? Really You are making others to know the code best practices solved by you.I am the one among them and its very helpfull to others to learn coding and  Analyse the errors. 
I Have one scenario like I have a picklist field and in that A,B,C are the pick list values and I am having users U!,U2,U3.I need to assign Picklist values as A-U1,B-U2,C-U3 how i can achieve this?Thanks in advance
hitesh90hitesh90
Hi Shekhar,

You can full fill your requirement using Recprd type. see below link:
https://help.salesforce.com/HTViewHelpDoc?id=customize_recordtype.htm

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
s shekar 20s shekar 20
Thanks For your Kind Reply but not using Record types by using apex coding