0

Controller Code -

public class ApplicationController {

    public String selectedApplicationId {get;set;}
    public static List<SelectOption> applicationOptions {
    get {
        if(applicationOptions == null) {
            applicationOptions = new List<SelectOption>(); 
            applicationOptions.add(new SelectOption('', 'None'));
        }
        return applicationOptions;
    } set;
}

    public static List<Application__c> apps{
    get {
        if (apps == null) {
            apps = new List<Application__c>();
        }
        return apps;
    }
    set;
}

public ApplicationController() {
    //basically get the contact ID from parameter
    contactID = parameters.get(CONTACT_ID);
     apps = [Select t.Id, t.Type__c, t.Name From Application__c t where t.contactID =: contactID];

        for(Application__c app : apps) {
            applicationOptions.add(new SelectOption(app.Id, app.Type__c + ' - ' + app.Name));
        }
}

public PageReference goToAnotherPage() {
//do something with the selected application ID
}

}

So on my page I have a button which I hit to get the selected application ID. But every time I hit the button it says

"applicationOptions: Validation Error: Value is not valid"

and the drop down is re-rendered with just None as its only value.

Can someone please guide me where am I going wrong? All the examples that I have seen have options being created as part of the getter/setter but I need to dynamically populate the application options.

  • Where is the code for when you hit the button? It is hard to tell you what might be wrong if we cant see what it is doing. – CyberJus Apr 1 '15 at 23:35
  • Somehow when I edit the VF code, it displays as blank so I am pasting the code here for reference – Kiera Apr 2 '15 at 2:39
  • 1
    VF Page - <apex:pageBlockSection> <apex:selectList id = "applicationOptions" value = "{!selectedApplicationId}" multiselect="false" size="1"> <apex:outputLabel value="Application" for="applicationOptions"/> &nbsp; <apex:selectOptions value="{!applicationOptions}"/> </apex:selectList> </apex:pageBlockSection> <apex:pageBlock> <apex:commandButton action="{!goToAnotherPage}" value="Use Application"/> </apex:pageBlock> – Kiera Apr 2 '15 at 2:39
1

It seems like you have not initialized applicationOptions list in your constructor where you are adding values to select options, try initializing as

applicationOptions = new List<SelectOption>();  
| improve this answer | |
  • Do I have to do that despite of initializing it in the getter? Wouldn't that value of None go away? – Kiera Apr 2 '15 at 14:35
  • Btw, this did not work. – Kiera Apr 2 '15 at 14:56
0

I resolved this. Remove the static from applicationOptions and moved the lines from controller to populate the applicationOptions into the getter itself.

| improve this answer | |
0

Here is my implementation. Controller

public string countryId {get;set;}

public void Submit(){
//countryId will have the required value
}

public List<SelectOption> getCountries()
{
    List<selectOption> options = new List<selectOption>();
    try
    {
        for (Country__c c : [SELECT id, Name FROM Country__c])
        {
            options.add(new selectOption(c.id, c.Name));
        }
    }
    catch(Exception e)
    {
        ApexPages.addMessages(e);
    }       
    return options;
}

Visualforce Page

    <apex:form>
   <apex:selectList title="Select Country *" tabindex="11" id="ddlCountry" value="{!countryId}"  styleClass="ddlCountry required fs-18 color-blue3 fw-200 lh-22 ddlCombo" size="1" >
<ap</apex:selectList>
    <apex:commandButton immediate="false"  id="btnSubmit" styleClass="" value="Submit"  action="{!Submit}" />  
    </apex:form>

I have extracted above from my working page please ask if you have any difficulty implementing this.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.