[SalesForce] Creating Picklist based on a other text field value

I have a list of questions (150+). Each question has a particular type of answer and some of the answers can be picklists.
Now I want to create the response picklists based on the question, how can I do so?
I do not want the question to be a controlling picklist, as the number of questions can grow further.

Thanks for the help in advance.

Example 
Ques                      Response Type                         Value
What is your fav game?    Picklist                              Chess, Tennis, Checkers
What is your fav colour?  Picklist                              Red,Green,Blue, Yellow
Which is your fav food?   Picklist                              Indian, Chinese, Italian, Mexican

So I need to populate the picklist values based on the questions.
I have more than 150 questions. So making the question controlling picklist and the Value as dependent picklist does not seem to be a suitable solution.
How can I achieve this functionality?

Best Answer

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.

Related Topic