[SalesForce] Change value on picklist based on another picklist value changes

I want to change picklist value based on another picklist value changes. I have tired below this code. Why this code not working. Anybody please correct my code..

VF:

<apex:page standardController="Opportunity" extensions="">

    <apex:form id="form" >

        <apex:outputLabel >Channel Group:</apex:outputLabel>
        <apex:selectList id="countries" value="{!Opportunity.Channel_Group__c}" size="1">
            <apex:actionSupport event="onchange" action="{!setValue}" rerender="form"/>
            <apex:selectOptions value="{!ChannelGroup}"/> 
        </apex:selectList>

        <apex:outputLabel >Channel Group:</apex:outputLabel>
        <apex:selectList id="countriess" value="{!Opportunity.Channel_Type__c}" size="1">
            <apex:selectOptions value="{!ChannelType}"/>
        </apex:selectList>


    </apex:form>

</apex:page>

Controller:

public class Sample{

    Opportunity opp;
    public List<SelectOption> channelTypeOptions;

    public Sample(ApexPages.StandardController controller) {
        opp = (Opportunity) controller.getRecord();
    }

    public List<SelectOption> getChannelGroup()
    {
        List<SelectOption> channelGroupOptions = new List<SelectOption>();

        Schema.DescribeFieldResult fieldResult = Opportunity.Channel_Group__c.getDescribe();
        List<Schema.PicklistEntry> channelGroupValue = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry cg : channelGroupValue)
        {
            channelGroupOptions.add(new SelectOption(cg.getLabel(), cg.getValue()));
        }       
        return channelGroupOptions;
    }

    public List<SelectOption> getChannelType()
    {
        channelTypeOptions = new List<SelectOption>();

        Schema.DescribeFieldResult fieldResult = Opportunity.Channel_Type__c.getDescribe();
        List<Schema.PicklistEntry> channelTypeValue = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry ct : channelTypeValue)
        {
            channelTypeOptions.add(new SelectOption(ct.getLabel(), ct.getValue()));
        }       
        return channelTypeOptions;
    }
    public void setValue(){
        if(opp.Channel_Group__c == 'Direct'){
            channelTypeOptions.add(new SelectOption('Outside Sales','Outside Sales'));
        }
    }
}

Best Answer

You can use this code by using the List of Selectoption in your visualforce page directly. See the modified code below.

<apex:page standardController="Opportunity" extensions="Sample">

    <apex:form id="form" >

        <apex:outputLabel >Channel Group:</apex:outputLabel>
        <apex:selectList id="countries" value="{!Opportunity.Channel_Group__c}" size="1">
            <apex:actionSupport event="onchange" action="{!setValue}" rerender="picklist"/>
            <apex:selectOptions value="{!ChannelGroup}"/> 
        </apex:selectList>
<apex:outputpanel id="picklist">
        <apex:outputLabel >Channel Group:</apex:outputLabel>
        <apex:selectList id="countriess" value="{!Opportunity.Channel_Type__c}" size="1">
            <apex:selectOptions value="{!channelTypeOptions}"/>
        </apex:selectList>
</apex:outputpanel>

    </apex:form>

</apex:page>

Controller

public class Sample{

    Opportunity opp;
    public List<SelectOption> channelTypeOptions{get;set}

    public Sample(ApexPages.StandardController controller) {
        opp = (Opportunity) controller.getRecord();
        getChannelType();
    }

    public List<SelectOption> getChannelGroup()
    {
        List<SelectOption> channelGroupOptions = new List<SelectOption>();

        Schema.DescribeFieldResult fieldResult = Opportunity.Channel_Group__c.getDescribe();
        List<Schema.PicklistEntry> channelGroupValue = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry cg : channelGroupValue)
        {
            channelGroupOptions.add(new SelectOption(cg.getLabel(), cg.getValue()));
        }       
        return channelGroupOptions;
    }

    public List<SelectOption> getChannelType()
    {
        channelTypeOptions = new List<SelectOption>();

        Schema.DescribeFieldResult fieldResult = Opportunity.Channel_Type__c.getDescribe();
        List<Schema.PicklistEntry> channelTypeValue = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry ct : channelTypeValue)
        {
            channelTypeOptions.add(new SelectOption(ct.getLabel(), ct.getValue()));
        }       
        return channelTypeOptions;
    }


       public void setValue(){
            if(opp.Channel_Group__c == 'Direct'){
                channelTypeOptions = new List<SelectOption>();
                channelTypeOptions.add(new SelectOption('Outside Sales','Outside Sales'));
            }else{
              getChannelType();
          }
        }
    }
Related Topic