0

I am trying to understand the usage of SelectList tag.

Below is the code that I expect to give me the selected Value from all the list of values in the drop down. Populating the dropdown dynamically with the list of recordTypes. But am unable to get the value selected by the user. I have also tried adding the actionsupport tag to see if on change event it will call the Apex Method and execute the debug statement under fetchVAL(), but I don't see it to be executing as well. Please advise how I can fetch the value of selected Record Type and do we need the action support tag here?

<apex:page controller="chooseColor">
<apex:form>
     <apex:selectList id="chooseRecType" value="{!RecType}"  size="1">
         <apex:actionSupport event="onchange" rerender="list" action="{!fetchVAL}"/>
         <apex:selectOptions value="{!RecTypeVals}"> 
         </apex:selectOptions>
    </apex:selectList> 
    <p id= "list">display : {!a}</p>
</apex:form>   

public class chooseColor {
    public string a {
        get {
            return RecType;
        }
        set;
    }

    public string RecType {
        get;
        set {
            a = RecType;
            system.debug('a===' + a);
        }
    }

    public List < SelectOption > RecTypeVals {
        get {
            List < SelectOption > SOp = new List < SelectOption > ();

            for (RecordType rt: [SELECT Id, Name FROM RecordType where SobjectType = 'Opportunity']) {
                system.debug('rt : ' + rt);
                SOp.add(new SelectOption(rt.name, rt.name));
            }

            system.debug('options : ' + SOp);
            return SOp;
        }
    }

    public void fetchVAL() {
        system.debug('RecType : ' + RecType); // to check if the selected Value from optionlist is captured.
    }
}

1 Answer 1

2

Your setter method for RecType is implemented incorrectly, which results in it having no effect and failing to store data.

A setter method receives an implicit argument called value, which it typically should assign to its own instance variable. Instead, you're assigning the existing value of the instance variable RecType (which will simply be null) to a different property a. a's own getter method returns the value of RecType, which is still null because the setter did not set it properly.

The end result is that the RecType property will always be null, so both it and a will appear blank in Visualforce. Easy fix: remove a completely, since you don't need it, and declare RecType:

public string RecType { get; set; }

For more background, see Apex Properties.

Please note that formatting your code well can make issues like this much easier to spot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .