+ Start a Discussion
hkp716hkp716 

Dependent Picklist on Vforce Page

Hi Everyone,

 

I'm new to VisualForce Pages and Salesforce in general.  I need an visual page that shows a dependent picklist from an object.  The picklist goes down 3 levels.  My custom fields are brand, product line, and subproduct line. Right now I can see the page but my picklists have no values.  I know I need a controller and maybe a wrapper class but im not sure where to insert it.

 

*I found this code online and just tweaked as per my specs.

 

My Controller 

 

public with sharing class MyController {

public string currentBrand { get; set; }
public string currentPline { get; set; }
public string currentSpline { get; set; }

public MyController() {
currentBrand = currentPline = currentSpline = null;
}

 

public List<SelectOption> getListBrand() {
List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
for(Schema.PicklistEntry pe:Product2.Family.getDescribe().getPicklistValues()) {
options.add(new SelectOption(pe.getValue(),pe.getLabel()));
}
return options;
}


public List<SelectOption> getListPline() {
List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
for(Schema.PicklistEntry pe:Product2.Family.getDescribe().getPicklistValues()) {
options.add(new SelectOption(pe.getValue(),pe.getLabel()));
}
return options;
}

public List<SelectOption> getListSpline() {
List<SelectOption> options = new List<SelectOption>();
if(currentPline == null || currentPline == '')
return options;
options.add(new SelectOption('','-- Choose --'));
for(Product2 p2:[select id,name from product2 where family = :currentPline]) {
options.add(new SelectOption(p2.id,p2.name));
}
return options;
}

}

 

VForce page:


<apex:page controller="MyController">
<apex:form id="theForm">
<apex:sectionHeader title="Choose Product Line"/>
<apex:pageBlock title="Demo Page">
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel >Product Line</apex:outputLabel>
<apex:selectList size="1" multiselect="false" value="{!currentPline}">
<apex:selectOptions value="{!ListPline}"/>
<apex:actionSupport reRender="theForm" event="onchange"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Sub-Product</apex:outputLabel>
<apex:selectList value="{!currentSpline}" size="1" multiselect="false">
<apex:selectOptions value="{!listProducts}"/>
<apex:actionSupport reRender="theForm" event="onchange"/>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Can anyone help me out?

Thanks for the help in advance,

Hkp716

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Are the picklist dependent on each other on object level?

 

If yes then you only need to use inputfields and it will be done automatically.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

Are the picklist dependent on each other on object level?

 

If yes then you only need to use inputfields and it will be done automatically.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
hkp716hkp716

Thanks Ankit,

 

Worked like a charm.  I've come across another issue now.  My 3rd pick list is dependent on my second picklist and it works fine, but my second picklist is not dependent on my first.  Is there a way set up a query in my controller or vforce page where values in my second picklist only populate when when a certain value in my 1st pick list is selected? (Without using field dependancies)

 

Some brands only have 1 or 2 product lines but instead all the product line show up in the picklist.

 

Thank again for your help,

-Hersh

 

My Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public class brands
{
public brands(ApexPages.StandardController controller)
{

}
public List<SelectOption> getTypes()
{
Schema.sObjectType sobject_type = product2.getSObjectType();
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();

List<Schema.PicklistEntry> pick_list_values = field_map.get('Select_Brand__c').getDescribe().getPickListValues();
List<selectOption> options = new List<selectOption>();
for (Schema.PicklistEntry a : pick_list_values)
{
options.add(new selectOption(a.getLabel(), a.getValue()));
}
return options;
}

}

 

 

 <apex:page standardController="product2" extensions="brands">

<apex:form >

<apex:pageBlock title="Floor Planning" mode="edit">

<apex:pageBlockSection columns="1">

<apex:inputField value="{!product2.Brand__c}">

<apex:inputField value="{!product2.Product_Line__c}" />

<apex:inputField value="{!product2.Sub_Product_Line__c}" />

</apex:inputField>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:page>

Aki99Aki99

Hey,

Were you finally able to do that with your first and second picklist?

If so please share the code

I am also facong the same problem