+ Start a Discussion
ThatGuy2013ThatGuy2013 

Dynamic Dependent Picklists

I'm trying to create a set of picklists. The first list displays a list of record names (duplicate names are not listed), and the second picklist is supposed to display all the records related to the record selected in the first list (duplicate names are not listed). However, I cannot get the second picklist to populate correctly. I've looked into the examples provided by Salesforce, and I cannot see what I'm doing wrong. Any help would be greatly appreciated, thanks!

 

VisualForce Page:

<apex:page controller="FieldUpdaterController">
<apex:form >
    <apex:pageBlock title="Field Update Module">
        <apex:pageBlockSection columns="1">
        {!selectedDDP}
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Invoice Statement"/>
                <apex:selectList value="{!selectedInvoice}" size="1">
                    <apex:selectOptions value="{!availableInvoices}" />
                    <apex:actionSupport event="onchange" rerender="itemPicklist" />
                </apex:selectList>              
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Line Item" />
                    <apex:selectList id="itemPicklist" value="{!selectedItem}" size="1">
                        <apex:selectOptions value="{!availableItems}" />
                    </apex:selectList>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

APEX:

public class FieldUpdaterController {

    public String selectedItem {get; set;}
    
    public string selectedInvoice {get; set;}
    
    public List<selectOption> getAvailableInvoices() {
    
        List<selectOption> optionalInvoices = new List<selectOption>();
        
        List<Invoice_Statement__c> allInvoices = [SELECT Name, Id FROM Invoice_Statement__c ORDER BY Name];
            
        string prevInvoice;
        
        for (Invoice_Statement__c i : allInvoices) {
            if(i.Name != prevInvoice) {
                optionalInvoices.add(new selectOption('',i.Name));
            }
            prevInvoice = i.Name;
        }
        
        if (optionalInvoices.size() == 0) {
        optionalInvoices.add(new selectOption('', 'No Invoices Available'));
        return optionalInvoices;
        }
        else{
        return optionalInvoices;
        }
        
    }
    
    public List<selectOption> getAvailableItems() {
        
        List<selectOption> availableItems = new list<selectOption>();
        
        string prevItem;              
 
        for(Line_Item__c li : [SELECT Name, Id FROM Line_Item__c WHERE Invoice_Statement__r.Name = :selectedInvoice]) {
            if(li.Name != prevItem) {
                availableFields.add(new selectOption('', li.Name));
            }
            prevItem = li.Name;
        }
               
        return availableItems;
    }
    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
lrw7572lrw7572

You are not passing the controller any value when the Invoice is selected. Right now you are doing "new selectOption("",i.Name)" - This displays the name to the user but passes an empty string to the conroller. Change that to "new SelectOption(i.Name,i.Name)" and the name will be passed. Also, you should change your "getAvailableItems" function to a pagereference and call it as an action in your actionSupport.

All Answers

lrw7572lrw7572

You are not passing the controller any value when the Invoice is selected. Right now you are doing "new selectOption("",i.Name)" - This displays the name to the user but passes an empty string to the conroller. Change that to "new SelectOption(i.Name,i.Name)" and the name will be passed. Also, you should change your "getAvailableItems" function to a pagereference and call it as an action in your actionSupport.

This was selected as the best answer
Gunners_23Gunners_23

Try reRendering the outputPanel instead of selectlist directly.

 

Also, in the below code snippet you're adding it to availableFields list and returning availableItems, guess typo mistake

 

 

if(li.Name != prevItem) {
                availableFields.add(new selectOption('', li.Name));
            }
            prevItem = li.Name;
        }
               
        return availableItems;

ThatGuy2013ThatGuy2013
This worked wonderfully.

However, what will changing getAvailableItems() to a pagereference accomplish? Sorry, I'm pretty new to this.