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
Bryan TelfordBryan Telford 

Apex class and VF page with actionsupport

I have the following Apex class and VF page.  Based on the selection of the tite, the corresponding role should display. I am using the actionSupport to pass the selected value onchange event, and assign the value to selectedTitle. But the selectedTitle is null everytime. Any ideas?

Apex class:
public class selectListController
{
    public String title { get; set; }
    public String role { get; set; }    
    public String selectedTitle { get; set; }        
    public List<selectOption> titleList { get; set; }
    public List<selectOption> roleList { get; set; }
    
    public selectListController()
    {
        titleList = new List<selectOption>();
        titleList.add(new SelectOption('', '--None--'));
        for (TitleRoleAssociation__c title : [Select Title_Definition__r.Name from TitleRoleAssociation__c]) {
            titleList.add(new selectOption(title.Title_Definition__r.Id, title.Title_Definition__r.Name));
        }
    }
    
    public PageReference getRoles(){
        roleList = new List<selectOption>();
        roleList.add(new SelectOption('', '--None--'));
        System.debug('Selected title: ' + selectedTitle);
        for (TitleRoleAssociation__c role : [Select Role_Definition__r.Name from TitleRoleAssociation__c where Title_Definition__r.Name = :selectedTitle]) {
            roleList.add(new selectOption(role.Role_Definition__r.Id, role.Role_Definition__r.Name));
        }
    
        return null;
    }
    
}

VF page:
<apex:page controller="selectListController">
    <apex:form>
        <apex:selectList value="{!title}" multiselect="false" size="1">
            <apex:selectOptions value="{!titleList}" />
            <apex:actionSupport event="onchange" action="{!getRoles}" reRender="role" status="status">                         
               <apex:param name="selectedTitle" value="{!title}" assignTo="{!selectedTitle}"/>
            </apex:actionSupport>            
        </apex:selectList>

        <apex:outputPanel id="role">
        <apex:selectList value="{!role}" multiselect="false" size="1">
            <apex:selectOptions value="{!roleList}" />
        </apex:selectList>        
        </apex:outputPanel>
    </apex:form>
</apex:page>
 
Best Answer chosen by Bryan Telford
Ajay K DubediAjay K Dubedi
Hi Bryan,
    
I have removed errors in your code and it is working fine in my org.
Please update your code by below code.

Controller.
public class selectListController
{
    public String title { get; set; }
    public String role { get; set; }    
    public String selectedTitle { get; set; }        
    public List<selectOption> titleList { get; set; }
    public List<selectOption> roleList { get; set; }
    
    public selectListController()
    {
        titleList = new List<selectOption>();
        titleList.add(new SelectOption('', '--None--'));
        for (TitleRoleAssociation__c title : [Select Title_Definition__r.Name from TitleRoleAssociation__c]) {
            titleList.add(new selectOption(title.Title_Definition__r.Id, title.Title_Definition__r.Name));
        }
    }
    
    public PageReference getRoles(){
        roleList = new List<selectOption>();
        roleList.add(new SelectOption('', '--None--'));
        selectedTitle =  title;
        System.debug('Selected title: ' + selectedTitle);
        for (TitleRoleAssociation__c role : [Select Role_Definition__r.Name from TitleRoleAssociation__c where Title_Definition__r.Name = :selectedTitle]) {
            roleList.add(new selectOption(role.Role_Definition__r.Id, role.Role_Definition__r.Name));
        }
    
        return null;
    }
    
}

VF Page:
<apex:page controller="selectListController">
    <apex:form>
        <apex:selectList value="{!title}" multiselect="false" size="1">
            <apex:selectOptions value="{!titleList}" />
            <apex:actionSupport event="onchange" action="{!getRoles}" reRender="role" status="status">                         
            </apex:actionSupport>            
        </apex:selectList>
        <apex:outputPanel id="role">
        <apex:selectList value="{!role}" multiselect="false" size="1">
            <apex:selectOptions value="{!roleList}" />
        </apex:selectList>        
        </apex:outputPanel>
    </apex:form>
</apex:page>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Bryan,
    
I have removed errors in your code and it is working fine in my org.
Please update your code by below code.

Controller.
public class selectListController
{
    public String title { get; set; }
    public String role { get; set; }    
    public String selectedTitle { get; set; }        
    public List<selectOption> titleList { get; set; }
    public List<selectOption> roleList { get; set; }
    
    public selectListController()
    {
        titleList = new List<selectOption>();
        titleList.add(new SelectOption('', '--None--'));
        for (TitleRoleAssociation__c title : [Select Title_Definition__r.Name from TitleRoleAssociation__c]) {
            titleList.add(new selectOption(title.Title_Definition__r.Id, title.Title_Definition__r.Name));
        }
    }
    
    public PageReference getRoles(){
        roleList = new List<selectOption>();
        roleList.add(new SelectOption('', '--None--'));
        selectedTitle =  title;
        System.debug('Selected title: ' + selectedTitle);
        for (TitleRoleAssociation__c role : [Select Role_Definition__r.Name from TitleRoleAssociation__c where Title_Definition__r.Name = :selectedTitle]) {
            roleList.add(new selectOption(role.Role_Definition__r.Id, role.Role_Definition__r.Name));
        }
    
        return null;
    }
    
}

VF Page:
<apex:page controller="selectListController">
    <apex:form>
        <apex:selectList value="{!title}" multiselect="false" size="1">
            <apex:selectOptions value="{!titleList}" />
            <apex:actionSupport event="onchange" action="{!getRoles}" reRender="role" status="status">                         
            </apex:actionSupport>            
        </apex:selectList>
        <apex:outputPanel id="role">
        <apex:selectList value="{!role}" multiselect="false" size="1">
            <apex:selectOptions value="{!roleList}" />
        </apex:selectList>        
        </apex:outputPanel>
    </apex:form>
</apex:page>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Bryan TelfordBryan Telford
Hi Ajay,

Perfect. Thank you!

Bryan