[SalesForce] Need to show multiselect picklist field as checkboxes in vf page?Any suggestions plz

i Need to show multiselect picklist field as checkboxes in vf page?Any suggestions plz.
and also want to save the value using controller.

Best Answer

Off the top of my head...

Take a look at the Describe API. From there you can get the available picklist values.

Schema.DescribeFieldResult dfr = Your_Object__c.Your_Field__c.getDescribe();
List<Schema.PicklistEntry> pickListEntries = dfr.getPicklistValues();

You could create a wrapper class to more easily be able to determine the transformation to and from the multiple fields.

public class CheckboxOption {
   public Boolean selected { get; set; }
   public String label { get; set; }
   public String value { get; set; }

}

From there you can look at building up your own List of CheckboxOptions from the List of Schema.PickListEntry.

List<CheckboxOption> options = new List<CheckboxOption>();
// Iterate over List of pickListEntries creating a CheckboxOption for each.

You also want to make sure that you correctly set whether the option was already selected, if loading an existing record. You can loop over the List of CheckboxOptions and set the selected Boolean appropriately. Take a look at Querying Multi-Select Picklists if you don't already have access to the value.

Then in the page you can iterate over the List of CheckboxOption writing out a label and inputCheckbox for each, using the selected Boolean for the inputCheckbox.

Finally, in your save method you can iterate over the List of CheckboxOptions building up a semi-colon delimited String from the selected options. Then you can set that as the value for you sObject's multi-select field.

One thing you will have to consider is what to do if the available values of the picklist change. Will you ever have a situation where one of the values on a record is no longer available to select? If so, you may want to handle that some way such as displaying it on the page but not allowing it to be selected so that on the Save it goes away. Maybe you will never have that situation, though.

Related Topic