1

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'));
        }
    }
}
5
  • Have you tried to create dependent picklist and just display both of them on a page? In your code you're just displaying possible values. To get dependency from schema requires additional magic Commented Aug 17, 2016 at 5:20
  • Yes I have tired dependent picklist
    – Jana Shree
    Commented Aug 17, 2016 at 5:24
  • And what was your experience? Commented Aug 17, 2016 at 5:24
  • Which place i made mistake I don't know..
    – Jana Shree
    Commented Aug 17, 2016 at 5:25
  • I'm newbie for salesforce..
    – Jana Shree
    Commented Aug 17, 2016 at 5:28

3 Answers 3

0

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();
          }
        }
    }
1
  • 1
    Close, but you forgot to reset the list during setValue, so the last item would get duplicated each time the condition was met.
    – sfdcfox
    Commented Aug 17, 2016 at 5:53
1

You're wiping out the entire pick list each time; this causes you to lose the work that you're doing in the "setValue" function. Instead, consider just adding it directly:

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()));
    }
    if(opp.Channel_Group__c == 'Direct'){
        channelTypeOptions.add(new SelectOption('Outside Sales','Outside Sales'));
    }

    return channelTypeOptions;
}

Functional, self-contained example:

public class dpl {
    public String valueA { get; set; }
    public String valueB { get; set; }

    public SelectOption[] getListA() {
        return new SelectOption[] {
            new SelectOption('a','a'),
            new SelectOption('b','b')
        };
    }

    public SelectOption[] getListB() {
        SelectOption[] values = new SelectOption[] {
            new SelectOption('1','1'),
            new SelectOption('2','2')
        };
        if(valueA == 'b') {
            values.add(new SelectOption('3','3'));
        }
        return values;
    }
}

<apex:page controller="dpl">
    <apex:form id="form">
        <apex:pageBlock>
            <apex:pageBlockSection columns="1">
                <apex:selectList size="1" multiselect="false" value="{!valueA}">
                    <apex:selectOptions value="{!listA}" />
                    <apex:actionSupport event="onchange" rerender="form" />
                </apex:selectList>
                <apex:selectList size="1" multiselect="false" value="{!valueB}">
                    <apex:selectOptions value="{!listB}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
4
  • Hi, Picklist value not change.. My VF page code is that correct?
    – Jana Shree
    Commented Aug 17, 2016 at 5:32
  • @JanaShree Off the top of my head, I think it should be "onselect" instead of "onchange". Could you try that?
    – sfdcfox
    Commented Aug 17, 2016 at 5:35
  • It's not working..
    – Jana Shree
    Commented Aug 17, 2016 at 5:38
  • @JanaShree I've added a self-contained example. Use "onchange", as you are, but don't try mixing class level variables with local getter methods or something like that. Note that there is no action method on my version at all; the getter takes care of everything. This isn't the only possible pattern, but it is lightweight on view state size compared to other methods.
    – sfdcfox
    Commented Aug 17, 2016 at 5:52
0

Goto to object definition, create two picklists, assign dependency between them:

enter image description here

Dependent picklist will be mapped automatically:

enter image description here

VF: 

<apex:page standardController="Opportunity" >
    <apex:form id="form" >
        <apex:inputField value="{!Opportunity.Channel_Group__c}"/>
        <apex:inputField value="{!Opportunity.Channel_Type__c}"/>
    </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .