function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sascha DeinertSascha Deinert 

Picklist remains blank after onchange

Hi, 

I don't know what is wrong with my code.
If I change the selectedrtype, the page should run the onchange event to get the stagelist, but the stagelist remains blank.

Thanks,
Sascha

 
Public class depen002_class {

Public String SelectedRType {get; set;}

Public depen002_class() {

}

Public List <SelectOption> getRTypeList() {
    List <RecordType > RTypes = [SELECT Id, Name FROM RecordType WHERE sObjectType = 'Opportunity'];
    List <SelectOption> RTypeOptionList = new List <SelectOption>();
    FOR (RecordType R : RTypes) {
        RTypeOptionList.add (new SelectOption(R.Id, R.Name));
    }
    RETURN RTypeOptionList; 
}

Public List <SelectOption> getStageList() {
    
    List <SelectOption> StageOptionList = new List <SelectOption>();     
        
        IF (SelectedRType == '') {
            StageOptionList.add (new SelectOption('- None -', 'Stages empty'));
        } ELSE {
            StageOptionList.add (new SelectOption('- None -', 'You found a Stage'));
        } 
         
    RETURN NULL;               
    }
     
}
 
<apex:page controller="depen002_class">

<apex:form >

<table width="100%">
    <tr>
        <td align="center">
            <apex:selectList value="{!SelectedRType}" size="1" multiselect="false" style="width:150px">
             <apex:actionSupport event="onchange" action="{!getStageList}" />
                <apex:selectOptions value="{!RTypeList}" />             
            </apex:selectList>
        </td>        
    </tr>
</table>


<table width="100%">
    <tr>
        <td align="center">
            <apex:selectList size="1" multiselect="false" style="width:150px">
                <apex:selectOptions value="{!StageList}" />             
            </apex:selectList>
        </td>        
    </tr>
</table>

{!SelectedRType}

</apex:form>

</apex:page>

 
Best Answer chosen by Sascha Deinert
Neetu_BansalNeetu_Bansal
Hi Sascha,

Use this page and controller class:
public class depen002_class
{
	public String SelectedRType {get; set;}
	public List<SelectOption> StageOptionList { get; set; }
	
	public depen002_class()
	{
	}

	public List <SelectOption> getRTypeList()
	{
		List <RecordType > RTypes = [SELECT Id, Name FROM RecordType WHERE sObjectType = 'Opportunity'];
		List <SelectOption> RTypeOptionList = new List <SelectOption>();
		for( RecordType R : RTypes )
		{
			RTypeOptionList.add (new SelectOption(R.Id, R.Name));
		}
		return RTypeOptionList; 
	}

	public void createStageList()
	{
		StageOptionList = new List <SelectOption>();     
        
        IF(SelectedRType == '') {
            StageOptionList.add (new SelectOption('- None -', 'Stages empty'));
        }
		ELSE {
            StageOptionList.add (new SelectOption('- None -', 'You found a Stage'));
        }              
    }
}
<apex:page controller="depen002_class">
	<apex:form >

		<table width="100%">
			<tr>
				<td align="center">
					<apex:selectList value="{!SelectedRType}" size="1" multiselect="false" style="width:150px">
						<apex:actionSupport event="onchange" action="{!createStageList}" reRender="stageId"/>
						<apex:selectOptions value="{!RTypeList}" />             
					</apex:selectList>
				</td>        
			</tr>
		</table>
		
		<apex:outputPanel id="stageId" >
			<table width="100%">
				<tr>
					<td align="center">
						<apex:selectList size="1" multiselect="false" style="width:150px">
							<apex:selectOptions value="{!StageOptionList}" />             
						</apex:selectList>
					</td>        
				</tr>
			</table>
			{!SelectedRType}
		</apex:outputPanel>
	</apex:form>
</apex:page>
Let me know, if you need any other help.

Thanks,
Neetu
 

All Answers

Neetu_BansalNeetu_Bansal
Hi Sascha,

After you may any call to controller, you need to reRender the section, to reflect the changes. Use below code for your visualforce page:
<apex:page controller="depen002_class">
	<apex:form >

		<table width="100%">
			<tr>
				<td align="center">
					<apex:selectList value="{!SelectedRType}" size="1" multiselect="false" style="width:150px">
						<apex:actionSupport event="onchange" action="{!getStageList}" reRender="stageId"/>
						<apex:selectOptions value="{!RTypeList}" />             
					</apex:selectList>
				</td>        
			</tr>
		</table>
		
		<apex:outputPanel id="stageId" >
			<table width="100%">
				<tr>
					<td align="center">
						<apex:selectList size="1" multiselect="false" style="width:150px">
							<apex:selectOptions value="{!StageList}" />             
						</apex:selectList>
					</td>        
				</tr>
			</table>
			{!SelectedRType}
		</apex:outputPanel>
	</apex:form>
</apex:page>
Let me know, if you need any other help.

Thanks,
Neetu
Sascha DeinertSascha Deinert
Still not works.

But if I change return of the stagelist from RETURN NULL to RETURN STAGELIST then I get the information "you found a stage".
But if I change the recordtype I get an error: Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexListELAdapter.

Could you help me again.

Thanks,
Sascha
Neetu_BansalNeetu_Bansal
Hi Sascha,

Use this page and controller class:
public class depen002_class
{
	public String SelectedRType {get; set;}
	public List<SelectOption> StageOptionList { get; set; }
	
	public depen002_class()
	{
	}

	public List <SelectOption> getRTypeList()
	{
		List <RecordType > RTypes = [SELECT Id, Name FROM RecordType WHERE sObjectType = 'Opportunity'];
		List <SelectOption> RTypeOptionList = new List <SelectOption>();
		for( RecordType R : RTypes )
		{
			RTypeOptionList.add (new SelectOption(R.Id, R.Name));
		}
		return RTypeOptionList; 
	}

	public void createStageList()
	{
		StageOptionList = new List <SelectOption>();     
        
        IF(SelectedRType == '') {
            StageOptionList.add (new SelectOption('- None -', 'Stages empty'));
        }
		ELSE {
            StageOptionList.add (new SelectOption('- None -', 'You found a Stage'));
        }              
    }
}
<apex:page controller="depen002_class">
	<apex:form >

		<table width="100%">
			<tr>
				<td align="center">
					<apex:selectList value="{!SelectedRType}" size="1" multiselect="false" style="width:150px">
						<apex:actionSupport event="onchange" action="{!createStageList}" reRender="stageId"/>
						<apex:selectOptions value="{!RTypeList}" />             
					</apex:selectList>
				</td>        
			</tr>
		</table>
		
		<apex:outputPanel id="stageId" >
			<table width="100%">
				<tr>
					<td align="center">
						<apex:selectList size="1" multiselect="false" style="width:150px">
							<apex:selectOptions value="{!StageOptionList}" />             
						</apex:selectList>
					</td>        
				</tr>
			</table>
			{!SelectedRType}
		</apex:outputPanel>
	</apex:form>
</apex:page>
Let me know, if you need any other help.

Thanks,
Neetu
 
This was selected as the best answer
SainSain
Hi,

Refer below code it may be helop you.

<apex:page sidebar="false" controller="FieldDependencyClass">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Country"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:SelectList value="{!Country}" size="1">
<apex:selectOptions value="{!countries}"/>
<apex:actionSupport event="onchange" rendered="B"/>
</apex:SelectList>
</apex:pageBlockSectionItem>
 
<apex:pageBlockSectionItem >
<apex:outputLabel value="State" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:selectList Value="{!State}" size="1" >
<apex:selectOptions value="{!states}" id="B"/>
<apex:actionSupport event="onchange" rendered="C"/>
</apex:selectList>
</apex:pageBlockSectionItem>
 
<apex:outputLabel value="City" />
<apex:selectList value="{!City}" size="1">
<apex:selectOptions value="{!Cities}" id="C"/>
</apex:selectList><br/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
 
public with sharing class FieldDependencyClass {
 
    Public String Country{get; set;}
    
    Public String States{get; set;}
    Public String Cities{get; set;}
    Public String Options{get; set;}
    //Public String StateOptions{get;set;}
    //Public String CityOptions{get;set;}
   
 
    public list<SelectOption> getCountries() {
    list<SelectOption> Options=new list<SelectOption>();
    Options.add(new SelectOption(' ','-None-'));
    Options.add(new SelectOption('IN','INDIA'));
    Options.add(new SelectOption('US','USA'));
        return Options;
    }
   
    public list<SelectOption> getStates() {
    list<SelectOption> Options=new list<SelectOption>();
    if(Country=='IN'||Country=='INDIA'){
    Options.add(new SelectOption('AP','Andhra Pradesh'));
    Options.add(new SelectOption('KN','Karnataka'));
    Options.add(new SelectOption('TN','TamilNadu'));
        }else   
    If(Country=='US'){
    Options.add(new SelectOption('CA','California'));
    Options.add(new SelectOption('TS','Texas'));
    Options.add(new SelectOption('CO','Colorado'));
       }else
       {
       Options.add(new SelectOption(' ','-None-'));
       }
    return Options;
    }
    public list<SelectOption> getCities() {
    list<SelectOption> Options=new list<SelectOption>();
    If(State=='KN'){
    Options.add(new SelectOption('BNR','Bangalore'));
    Options.add(new SelectOption('MYS','Mysor'));
    }else
    If(State=='AP'){
    Options.add(new SelectOption('TPT','Tirupati'));
    Options.add(new SelectOption('VNG','Vizag'));
    }else
    If(State=='TN'){
    Options.add(new SelectOption('CH','Chennai'));
    Options.add(new SelectOption('TRI','Trivendom'));
    }else
    If(State=='CA'){
    Options.add(new SelectOption('LAS A','LAS Angeles'));
    Options.add(new SelectOption('SAN J','San Jose'));
    }else
    If(State=='TS'){
    Options.add(new SelectOption('AU','Austin'));
    Options.add(new SelectOption('HS','Houston'));
    }else
    If(State=='CO'){
    Options.add(new SelectOption('DEN','Denver'));
    Options.add(new SelectOption('BL','Boulder'));
    }else
    {
    Options.add(new SelectOption(' ','-None-'));
    }
        return Options;
    }
}

Regards,
Sain