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
Krish NKrish N 

How to auto assign picklist value based on selected picklist value on VF form?

Hello everyone, I've created a VF form through the case object, from which, a technician will fill out the form based on customer input in reagrds to the product service issues. I've created two picklist fields for states and managers represeting those states. Whenever, the technician selects a state , I want the manager field to be auto assigned. How do I do that on the VF form? I don't want the technican to select the manager, I want it that to be auto populated based on the selected state.
Best Answer chosen by Krish N
Rahul.MishraRahul.Mishra
Here is the sample code for what you are looking for:
 
Page:

<apex:page controller="ctlPickLst">
    <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="Manager"/>
                </apex:pageblockSectionItem>            
                <apex:pageblockSectionItem >
                    <apex:selectList size="1" value="{!manager}" id="a">
                        <apex:selectOptions value="{!managers}"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>            
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>


Class:

public class ctlPickLst {
    public String manager {get;set;}
    public String state {get;set;}

    public List<SelectOption> getstates()
    {
        List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('None','None'));
        options.add(new SelectOption('KA','KA'));
        options.add(new SelectOption('TN','TN'));
		options.add(new SelectOption('MH','MH'));
        return options;
    } 
    
    public List<SelectOption> getManagers()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'KA')
        {       
	       	
            options.add(new SelectOption('Mr','Mr'));
			manager = 'Mr';

        }
        else if(state == 'TN')
        {       
	     	
            options.add(new SelectOption('Mishra','Mishra'));
			manager = 'Mishra';

        }
		    else if(state == 'MH')
        {       
            options.add(new SelectOption('Rahul','Rahul'));
			manager = 'Rahul';

        }
        else
        {
            options.add(new SelectOption('None','--- Select ---'));
        }      
        return options;
    }       
}

Mark answer as best if it does solve your problem

All Answers

Rahul.MishraRahul.Mishra
Here is the sample code for what you are looking for:
 
Page:

<apex:page controller="ctlPickLst">
    <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="Manager"/>
                </apex:pageblockSectionItem>            
                <apex:pageblockSectionItem >
                    <apex:selectList size="1" value="{!manager}" id="a">
                        <apex:selectOptions value="{!managers}"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>            
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>


Class:

public class ctlPickLst {
    public String manager {get;set;}
    public String state {get;set;}

    public List<SelectOption> getstates()
    {
        List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('None','None'));
        options.add(new SelectOption('KA','KA'));
        options.add(new SelectOption('TN','TN'));
		options.add(new SelectOption('MH','MH'));
        return options;
    } 
    
    public List<SelectOption> getManagers()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'KA')
        {       
	       	
            options.add(new SelectOption('Mr','Mr'));
			manager = 'Mr';

        }
        else if(state == 'TN')
        {       
	     	
            options.add(new SelectOption('Mishra','Mishra'));
			manager = 'Mishra';

        }
		    else if(state == 'MH')
        {       
            options.add(new SelectOption('Rahul','Rahul'));
			manager = 'Rahul';

        }
        else
        {
            options.add(new SelectOption('None','--- Select ---'));
        }      
        return options;
    }       
}

Mark answer as best if it does solve your problem
This was selected as the best answer
Krish NKrish N
Thanks for the reply. I will surely check it out. I'm relatively new to Salesforce, It may be a stupid question but wouldn't field dependencies work if I have state as a controlling field and manager field as a dependent field? 
Rahul.MishraRahul.Mishra
Hi Shashank,

If you use the dependent picklist here, value in dependent field will not be auto populated based on selection in controlling field, option will change based on selected value and after that value in dependent field you have to choose.

Thanks,
​Rahul
Krish NKrish N
Hey Rahul, Thank you for the answer. Quick question, So, Do I have to write the code fro entire states, there're like 50 states and 20 managers. Isn't there any workaround to simplify the result. Please let me know. Thanks!