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:
This will depend on your archetecture of the question record and your answers.
Lets assume the Object that holds your questions also holds the answers. You can also do this by having an answers object that has a MD relationship to the question object and iterate over those to build the lists.
As an example:
Question Object -> Question_Field__c, Answer_Field__c (Text), isPicklist__c
Then in your controller you populate your answers into the variable you define...
To populate the pick list for the questions with an isPicklist__c = true you would create a method to get the answers as a pick list:
public SelectOption[] populate_Answers(Question__c q){
String[] tmp = q.Answer_Field__c.split(';');
SelectOption[] ans = New SelectOption[]{};
for(String a : tmp){
ans.add(New SelectOption(a,a));
}
return ans;
}
Now, how you use this or populate it on your page you will need to figure out. A wrapper class may be necessary as well as conditional rendering on the VF page to either show a pick list, value, true false, etc.
Best Answer
Dependent picklists can only work on the same object. That means that if a custom object A__c has a master-detail relationship with B__c, then a picklist on B__c can't reference the picklist from A__c.