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
johndoejohndoe 

How to add dynamic select list

I want to create a dynamic select list. Below is my code:

 

Component.Apex.SelectList selectDynamic = new Component.Apex.SelectList();
        selectDynamic.multiselect = false;
        selectDynamic.value = stringList;
        Component.Apex.SelectOptions dynamicOptions = new Component.Apex.SelectOptions();
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('hi','hi'));
        dynamicOptions.value = options;
        selectDynamic.childComponents.add(dynamicOptions);

 

on the page load I'm getting error "Invalid selectOptions found. Use SelectOption type in Apex."

doughauck-corpdoughauck-corp
The SelectOption class found in the System namespace (i.e. the one you get by default) is NOT the same as the one required for Dynamic VF Components.  That's what that (poorly-worded) message is trying to say - you need to use Component.Apex.SelectOption instead.
I haven't been able to make it work with the SelectOptions component either, at least in Dynamic VF; I get the same message as you do.

Instead, create the individual Component.Apex.SelectOption objects and add them directly to the SelectList:
Component.Apex.SelectList selectDynamic = new Component.Apex.SelectList(size=1);
selectDynamic.childComponents.add(new Component.Apex.SelectOption(itemValue='hi', itemLabel='hi'));
selectDynamic.childComponents.add(new Component.Apex.SelectOption(itemValue='bye', itemLabel='bye'));
Don't forget that, for a SelectList, the multiSelect and size options should be specified in the constructor (if you want other than defaults).

Best,
Doug