Question

I am trying to create a class that will populate the a multi selectlist depending on what URL parameters are passed in.

Im having trouble. The slectlist never renders, however, looking through the debugger, i can see that records are found and that it should populate.

Am I missing something stupid?

VisualForce.Page

<apex:page controller="PopUp">

<apex:form >
    <apex:selectList value="{!objectType}"  multiselect="true">
        <apex:selectOptions value="{!SelectListOptions}"/>
    </apex:selectList>
</apex:form>

Controller class:

public with sharing class PopUp {

public sObject objParameter {get;set;}
public string fldParameter {get;set;}
public string queryType {get;set;}
public static string objName{get;set;}
public list<SelectOption> selectListOptions {get;set;}
public static list<string> TEST {get;set;}
public String[] objectType= new String[]{};

public PopUp(){

    objName = ApexPages.currentPage().getParameters().get('obj');
    sObject dynObject = Schema.getGlobalDescribe().get(objName).newSObject() ; 
    objParameter = dynObject;

    fldParameter = ApexPages.currentPage().getParameters().get('fld');
    String queryType = ApexPages.currentPage().getParameters().get('qt');

    system.debug('***objParameter: ' + objParameter);
    system.debug('***fldParameter: ' + fldParameter);

    //List<selectOption> L =  getPicklistValues(objParameter, fldParameter, queryType);
}

public static list<SelectOption> getPicklistValues(SObject obj, String fld, String queryType)
{
    list<SelectOption> options = new list<SelectOption>();

    if(queryType == 'soql'){

        string query = 'select ' + fld + ' from ' +  objName;
        system.debug('***query: ' + query);

        List<sObject> dynList = Database.query(query);
        system.debug('***dynList: ' + dynList);

        for(integer i=0;i< dynList.size();i++){

            string fldValue = string.valueof(dynList[i].get(fld));
            system.debug('***fldValue: ' + fldValue);

            options.add(new SelectOption(fldValue,fldValue));
            TEST.add(fldValue);
        }
    }

    system.debug('***options: ' + options);
    return options;

}

public List<selectOption> getSelectListOptions() {
    return getPicklistValues(objParameter, fldParameter, queryType);
}

public String[] getObjectType() 
{
    return objectType;
}

public void setObjectType(String[] objectType) 
{
    this.objectType= objectType;
} 
Was it helpful?

Solution

I think you're confusing the VF page by having essentially two getter methods (the one you added, and the one you told the controller to create itself by specifying get; in the variable declaration):

public list<SelectOption> selectListOptions {get;set;}

public List<selectOption> getSelectListOptions() {
    return getPicklistValues(objParameter, fldParameter, queryType);
}

It doesn't look like you're populating selectListOptions anywhere, so the VF page sees that and doesn't add any options to your picklist. The VF page will only call getSelectListOptions() if it doesn't find a getter for your variable, so modify the selectListOptions declaration like so, and you should be fine:

public List<SelectOption> selectListOptions {set;}

In fact, it doesn't look like your page should have access to set this, so you could remove the setter as well if you wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top