5

I created a list custom settings, and I need to add this into an existing picklist in my visualforce page.

I know how to display this list custom setting as a drop down in visualforce page, but I don't know how can I add this list into the existing picklist. When users select the picklist, and they can see this list custom settings as one of the picklist option.

1 Answer 1

7

My example based on the following settings:

  • Custom object: MyObject__c
  • A normal picklist: Picklist__c
  • Custom setting object: MySettingsList__c
  • Custom setting fields: Setting1 and Setting2

First i will read all values of the custom picklist and add it to the select option list. Then read the custom settings and add it to the list too.

Controller:

public String selectedValue { get; set; }

public List<SelectOption> getMyOptions(){ 
    List<SelectOption> options = new List<SelectOption>();      

    // Reading picklist values and labels
    Schema.DescribeFieldResult fieldResult = MyObject__c.Picklist__c.getDescribe();
    List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues();

    // Adding apicklist values to the select list
    for(Schema.PicklistEntry entry : picklistEntries){
        options.add(new SelectOption(entry.getValue(), entry.getLabel())); 
    }

    // Now adding custom settings to the select list
    for(MySettingsList__c setting : MySettingsList__c.getAll().values()){
        options.add(new SelectOption(setting.name, setting.Setting1__c)); 
        options.add(new SelectOption(setting.name, setting.Setting2__c)); 
    }     

    return options;
}

Visualforce page:

<apex:selectList value="{!selectedValue}" size="1" multiselect="false">
    <apex:selectOptions value="{!MyOptions}"/>
</apex:selectList>

Then it should look like this:

enter image description here

2
  • Is it possible to add the whole list custom setting into the drop down instead of the list custom setting fields? For example, I have two separate list custom settings, and each list will add into the drop down.
    – R.Spark
    Commented Feb 28, 2013 at 15:06
  • By using the above functionality we are getting as picklist but we need to display as in multiselect pick list.Here one is custom setting list and another is custom object multi-picklist value.I need to merge these two.
    – user5257
    Commented Mar 20, 2014 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .