[SalesForce] Get Selected Picklist Value

I've been trying to insert a custom setting record wherein role name selected from the dropdown menu must be the saved value in the Role_Name__c field. But every time record is inserted, role name does not have any value.

Apex Class:

public class SettingController {

    public WrapperSetting wrapSet {get; set;}
    public String selectedRoleValue {get;set;}

    public SettingController() {
        String wrapSetId = ApexPages.currentPage().getParameters().get( 'id' );

        if (wrapSetId != null ) {
            wrapSet = new WrapperSetting([SELECT Id, Name, TextField__c, Role_Name__c FROM Setting__c WHERE Id = :wrapSetId]);
        } else {
            wrapSet = new WrapperSetting();
        }
    }

    public List<SelectOption> getUserRoleList() {
        List<UserRole> userRoleList = [SELECT Id, Name FROM UserRole];
        List<SelectOption> uRoleOptionList = new List<SelectOption>();
        uRoleOptionList.add(new SelectOption(' ',' '));

        for(UserRole ur : userRoleList) {
            uRoleOptionList.add(new SelectOption(ur.Id, ur.Name));    
        }

        return uRoleOptionList;
    }


    public PageReference save() {
        if (wrapSet.Id != null) {
            update wrapSet.getSetting();
        } else {
            wrapSet.insertSetting();
        }

        PageReference pr = new PageReference('/' + wrapSet.Id);
        pr.setRedirect(true);
        return pr;
    }

    public class WrapperSetting {
        public Id Id {get;set;}
        public String Name {get;set;}
        public String TextField {get;set;}
        public String selectedRole {get;set;}

        public WrapperSetting(Setting__c s2) {
            this.Id = s2.Id;
            this.Name = s2.Name;
            this.TextField = s2.TextField__c;
            this.selectedRole = s2.Role_Name__c;

        }

        public WrapperSetting() {}

        public Setting__c getSetting() {
            return new Setting__c(
                Id = this.Id,
                Name = this.Name,
                TextField__c = this.TextField, 
                Role_Name__c = this.selectedRole

            );
        }

        public void insertSetting() {
            Setting__c s = this.getSetting();
            insert s;
            this.Id = s.Id;
        }

    }
}

Visualforce Page:

<apex:page controller="SettingController" sidebar="false" showHeader="false">
    <apex:pageMessages />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Name</apex:outputLabel>
                    <apex:inputText value="{!wrapSet.Name}"/>                
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Text Field</apex:outputLabel>
                    <apex:inputText value="{!wrapSet.TextField}"/>                
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Role Name</apex:outputLabel>
                    <apex:selectList value="{!selectedRoleValue}" size="1">
                        <apex:selectOptions value="{!UserRoleList}"/> 
                    </apex:selectList>                
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Best Answer

In your VF page, you are binding the selected role value from the selectList to the controller variable selectedRoleValue rather than the wrapper class variable selectedRole. if you change that, it should get saved.

change this

<apex:selectList value="{!selectedRoleValue}" size="1">

to

<apex:selectList value="{!wrapSet.selectedRole}" size="1">
Related Topic