I'm trying to show multi-picklist field on VF page. Ideally I should be able to show picklist values already selected and once user do selecting/unselecting some items, it should save back to database.
Below is my approach.
In controller class,
This Function provides generated select options for select radio/checkboxes in UI
public List<SelectOption> getSelectionOptions(Sobject Obj, String PicklistFieldName){
Schema.sObjectType sobject_type = Obj.getSObjectType();
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
List<Schema.PicklistEntry> pick_list_values = field_map.get(PicklistFieldName).getDescribe().getPickListValues();
List<selectOption> options = new List<selectOption>();
for (Schema.PicklistEntry a : pick_list_values) {
SelectOption so = new selectOption(a.getLabel(), a.getValue());
//if (Obj[PicklistFieldName].contains(a.getLabel()))
//so.setValue('true');
options.add(so);
}
return options;
}
// Get Options for multi-picklist
public List<SelectOption> getOptions(){
return getSelectionOptions(Obj,'multipickfield');
}
In VF Page,
<apex:selectCheckboxes value="{!Obj.multipickfield}" layout="pageDirection">
<apex:selectOptions value="{!Options}"></apex:selectOptions>
</apex:selectCheckboxes>
Checkboxes are displayed. But it's not correct the way it tries show the already selected values on VF page.
I'm not sure if this is the right way to achieve this. Anyone who has done it successfully, appreciate any guidance. Thanks.