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
Manjunath BNManjunath BN 

Display a Multipick list as checkbox in vf page

HI All,

 

I m trying to dispaly a multipicklist as checkboxes in vf page but not succeeding.

 

How can I display the picklists in below showed style.

 

Name of the field : testpick__c

values : a, b, c, d, e

Object name: test__c

 

Expected Output:

                                A                     B                        C                       D                      E

 

Testpick                   X                      X                        X                      X                        X 

 

 

Thanks in advance..........

Sonali BhardwajSonali Bhardwaj

Below can be one approach.

Call describe methods on your field test__c to get its pick list values. It will return you a, b, c, d, e. Then on your vf display check boxes for each returned value. Then in your apex, check what all values are checked and save them all in your field test__c.

Bhawani SharmaBhawani Sharma

This is what we did recently:

/**
     * Description  :   To use this variable on page, Use <apex:selectCheckBoxex>. Code will be:
     *   <apex:selectCheckboxes >
            <apex:selectOptions value="{!testCheckBoxOpotions}"/>
        </apex:selectCheckboxes>
     *
     * Check http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectCheckboxes.htm link for more details
     *
     **/
    //Method for the selection of How did you hear picklist values
    public List<SelectOption> HowDidYouHearOptions {
        get {
           
            //Describe SObject field and get the fields map
            Map<String, Schema.SObjectField> mapFields = Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap(); 
          
            //List of select option
            return Utility.getSelectOptionListByField(mapFields.get('How_did_you_hear__c'));        
        }
    }

 

Utility.class

//This method is to create a select option list for given Picklist Entries
	public static List<SelectOption> getSelectOptionListByField(Schema.sObjectField sObjectField) {
	  
		//Describe field and create a list of select options
	  	List<SelectOption> listOptions = new List<SelectOption>();
	  
	  	//Get picklist entries and interate to display these in form of checkboxes
	    List<Schema.PicklistEntry> pickListEntries = sObjectField.getDescribe().getPicklistValues();
	        
	    //loop throough the picklist entries and populate select list
	    for (Schema.PicklistEntry pE : pickListEntries) {
	         
	    	//Populate the select list with values
	        listOptions.add(new SelectOption(pE.getValue() , pE.getLabel()));  
	    }
	        
	    //Return list
		return listOptions;
	}

 

srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
Displaying Account   Industry picklist field as a check box manner

in visualforce page:

<apex:page controller="picklistascheck">
    <apex:form >
        <apex:pageblock >
            <apex:pageBlockSection columns="1">
                    <apex:pageblocksectionitem >         
                        <apex:outputlabel value="Industry" />
                        <apex:selectcheckboxes layout="pageDirection" value="{!IndustryName}">                  
                            <apex:selectoptions value="{!IndustryNames}" />         
                        </apex:selectcheckboxes>
                    </apex:pageblocksectionitem>
             </apex:pageBlockSection>
         </apex:pageblock>
     </apex:form>
</apex:page>

in apex class:

public List<String> IndustryName { get; set; }
    public picklistascheck()
    {
        IndustryName = new List<String>();
    }
    public List<selectoption> getIndustryNames()
        {          
            list<selectoption> options = new list<selectoption>();           
            try
            {              
            //Product Name is a MultiSelect Picklist              
            Schema.DescribeFieldResult fieldResult = Account..fields.Industry.getDescribe();
   
            list<schema.picklistentry> values = fieldResult.getPickListValues();              
            for (Schema.PicklistEntry a : values)
            {                 
            options.add(new SelectOption(a.getLabel(), a.getValue()));
            }          
            } 
            catch (Exception e)
            {            
            ApexPages.addMessages(e);          
            }
            system.debug('## Product Name Options'+ options);         
            return options;
        }
}

srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
check the code by puting debugs.so that you can know field values are comming or not .if any thing more elaborate you question
rajesh k 10rajesh k 10
Hi srilakshmi1,
                                All fields Edited in my visualforce page.But how to retrieve Multipicklist values as a checkboxes Dynamically.

srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
in page you have to place like this
<apex:selectcheckboxes layout="pageDirection" value="{!IndustryName}">                 
                            <apex:selectoptions value="{!IndustryNames}" />        
                        </apex:selectcheckboxes>
srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
place controller code fully
rajesh k 10rajesh k 10
Hi,
         Using below code i displaye all fields Dynamically.All fields will be displayed (Multipicklist also).I want how to find that multipicklist and display that mutipicklist as a checkboxes Dynamically?

Page:
<apex:commandButton value="Save" action="{!doSave}"/>
<apex:repeat value="{!listObjectFields}" var="fieldAPIName">
               <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
           </apex:repeat>
       </apex:pageBlockSection>

Controller:
----------------

public sObject sObjectToBind {get;set;}
    public List<String> listObjectFields {get;set;}

public void doSave()
{
if( sObjectToBind != null )
  insert sObjectToBind;
}
public DynamicBindingCls()
{

strtemp=ApexPages.currentPage().getParameters().get('DNF');
   string objectname ='rajesh__'+strtemp+'__c';
  
    listObjectFields =  new List<String>();
    Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
    Schema.sObjectType sObjType = globalDescription.get(objectname);
 
    //take care of the sequence of following 5 statements

    sObjectToBind = sObjType.newSObject();
    Schema.DescribeSObjectResult r1 = sObjType.getDescribe();
  
    List<User> lstUser = [ SELECT Id FROM User WHERE Username = 'rajesh060708@gmail.com' ];
if( sObjectToBind != null && lstUser != null && !lstUser.isEmpty() )
  sObjectToBind.put('OwnerId', lstUser[0].Id);

    Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();
    Integer i = 0;
    for(Schema.SObjectField field : mapFieldList.values())
    {
        Schema.DescribeFieldResult fieldResult = field.getDescribe();
        if(fieldResult.isAccessible() && fieldResult.isUpdateable())
        {
            listObjectFields.add(fieldResult.getName());
        }
    }      
}
srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
see this onces 
in page:

<apex:page controller="DynamicBindingCls">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:commandButton value="Save" action="{!doSave}"/>
                    <apex:repeat value="{!listObjectFields}" var="fieldAPIName" >
                        <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
                    </apex:repeat>
            </apex:pageBlockSection>
   
            <apex:selectcheckboxes layout="pageDirection" >                 
                <apex:selectoptions value="{!options}" />        
            </apex:selectcheckboxes>
        </apex:pageBlock>
   </apex:form>
</apex:page>


in controller:
public class DynamicBindingCls{
   
    public sObject sObjectToBind {get;set;}
    public List<String> listObjectFields {get;set;}
    public List<String> listObjectFields1 {get;set;}
    public List<String> PickListName { get; set; }
    public list<selectoption> options{get;set;}

    public void doSave()
    {
    if( sObjectToBind != null )
      insert sObjectToBind;
    }
    public DynamicBindingCls()
    {
        string objectname ='Account'; 
        listObjectFields =  new List<String>();
        listObjectFields1 =  new List<String>();
        PickListName = new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType sObjType = globalDescription.get(objectname);

        //take care of the sequence of following 5 statements

        sObjectToBind = sObjType.newSObject();
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();
 
        List<User> lstUser = [ SELECT Id FROM User WHERE Username = 'rajesh060708@gmail.com' ];
        if( sObjectToBind != null && lstUser != null && !lstUser.isEmpty() )
            sObjectToBind.put('OwnerId', lstUser[0].Id);

        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();
        Integer i = 0;
        for(Schema.SObjectField field : mapFieldList.values())
        {
            Schema.DescribeFieldResult fieldResult = field.getDescribe();
            if (fieldResult.getType() == Schema.DisplayType.PickList){
                listObjectFields1.add(fieldResult.getName());
            }
            else if(fieldResult.isAccessible() && fieldResult.isUpdateable())
            {
                listObjectFields.add(fieldResult.getName());
            }
        }
 
        options = new list<selectoption>();
        for(string pickval:listObjectFields1)
        {
            Schema.DescribeFieldResult fieldResult = Account.pickval.Industry.getDescribe();
            system.debug('33333333'+fieldResult);
            list<schema.picklistentry> values = fieldResult.getPickListValues();             
            for (Schema.PicklistEntry a : values)
            {                
                options.add(new SelectOption(a.getLabel(), a.getValue()));
            }         
            
        }
   
   }
}
rajesh k 10rajesh k 10
Hi  srilakshmi,
                             Not single field. i will create Every time create Multiselectpicklist  using  dynamic object on that time automatically in my visualforce page that multipicklist  visible as checkboxes Dynamically.

Note:Not perticular field.