+ Start a Discussion
Eric Anderson 54Eric Anderson 54 

Pick list not populated

Hi there everyone,

I'm new to Salesforce and Visualforce development. Though I have been doing C# for years in the Windows environment, my experience in web development is pretty limited. I am trying to build a solution where there are two picklists. The first pick list (OCS Units) is based on a 'Global pick list'. The second pick list should be loaded based on the value picked in the first pick list. When I run my code, it doesn't look like even the first pick list is being populated because when I click on the 'drop down' arrow, nothing is displayed.

Below is my Visualforce code:
 
<apex:page controller="OCSPicklistLoader">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
            	<apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Unit"/>
                </apex:pageBlockSectionItem>
            	<apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!OCS_Unit}">
                    	<apex:selectOptions value="{!OCS_Unit}"/>
                    	<apex:actionSupport event="onchange" reRender="Offices"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Office"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!OCS_Office}" id="Offices">
                    	<apex:selectOptions value="{!OCS_Office}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>    
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Below is my Controller code:
public class OCSPicklistLoader {
    public string OCS_Unit {get;set;}
    public string OCS_Office {get;set;}
    public List<OCS_Office__c> OCSOffices;
     
    public List<SelectOption> OCS_Unit()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('A','None'));
        options.add(new SelectOption('B','FAT'));
        options.add(new SelectOption('C','SSU'));
        options.add(new SelectOption('D','EOU'));
        options.add(new SelectOption('E','EPMU'));
        options.add(new SelectOption('F','CIAU'));
        options.add(new SelectOption('G','JGO'));
        return options;
    }
    public List<SelectOption> OCS_Office()
    {
        List<SelectOption> options = new List<SelectOption>();

        OCSOffices = [select Name from OCS_Office__c WHERE OCS_Unit_Code__c=:OCS_Unit]; 
        for(OCS_Office__c a : OCSOffices)
        {
            options.add(new SelectOption(a.Name,a.Name));
        } 
        return options;
		
    }
	
}
Any assistance would be greatly appreciated.

Thanks!

Eric
Best Answer chosen by Eric Anderson 54
Karan Shekhar KaulKaran Shekhar Kaul

Hi,
 

Try below code  



public class OCSPicklistLoader {
    public string selectedOCSUnit {get;set;}
    public string selectedOCSOffice {get;set;}
    public List<OCS_Office__c> OCSOffices;
     
    public List<SelectOption> getOCS_Unit()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('A','None'));
        options.add(new SelectOption('B','FAT'));
        options.add(new SelectOption('C','SSU'));
        options.add(new SelectOption('D','EOU'));
        options.add(new SelectOption('E','EPMU'));
        options.add(new SelectOption('F','CIAU'));
        options.add(new SelectOption('G','JGO'));
        return options;
    }
    public List<SelectOption> getOCS_Office()
    {
        List<SelectOption> options = new List<SelectOption>();

        OCSOffices = [select Name from OCS_Office__c WHERE OCS_Unit_Code__c=:selectedOCSUnit]; 
        for(OCS_Office__c a : OCSOffices)
        {
            options.add(new SelectOption(a.Name,a.Name));
        } 
        return options;
        
    }
    
}


=====================================================================================================


<apex:page controller="OCSPicklistLoader">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Unit"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!selectedOCSUnit}">
                        <apex:selectOptions value="{!OCS_Unit}"/>
                        <apex:actionSupport event="onchange" reRender="Offices"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Office"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!selectedOCSOffice}" id="Offices">
                        <apex:selectOptions value="{!OCS_Office}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>    
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

All Answers

sfdc Dsfdc D
Can you try this link. you have coded wrong. 

http://www.infallibletechie.com/2012/10/dependent-picklist-using-apex-in.html

 
Karan Shekhar KaulKaran Shekhar Kaul

Hi,
 

Try below code  



public class OCSPicklistLoader {
    public string selectedOCSUnit {get;set;}
    public string selectedOCSOffice {get;set;}
    public List<OCS_Office__c> OCSOffices;
     
    public List<SelectOption> getOCS_Unit()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('A','None'));
        options.add(new SelectOption('B','FAT'));
        options.add(new SelectOption('C','SSU'));
        options.add(new SelectOption('D','EOU'));
        options.add(new SelectOption('E','EPMU'));
        options.add(new SelectOption('F','CIAU'));
        options.add(new SelectOption('G','JGO'));
        return options;
    }
    public List<SelectOption> getOCS_Office()
    {
        List<SelectOption> options = new List<SelectOption>();

        OCSOffices = [select Name from OCS_Office__c WHERE OCS_Unit_Code__c=:selectedOCSUnit]; 
        for(OCS_Office__c a : OCSOffices)
        {
            options.add(new SelectOption(a.Name,a.Name));
        } 
        return options;
        
    }
    
}


=====================================================================================================


<apex:page controller="OCSPicklistLoader">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Unit"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!selectedOCSUnit}">
                        <apex:selectOptions value="{!OCS_Unit}"/>
                        <apex:actionSupport event="onchange" reRender="Offices"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="OCS Office"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:selectList size="1" value="{!selectedOCSOffice}" id="Offices">
                        <apex:selectOptions value="{!OCS_Office}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>    
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

This was selected as the best answer
Eric Anderson 54Eric Anderson 54
Karan,

Thank you very much for your clear and consise answer to this question. I now have a better (yet not perfect :-) ) understanding of how that should work. Thanks! - Eric -