Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks.I think i will definitely going to work !!! –  Pankaj Jan 24 '13 at 11:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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