Custom Actions: DynamicDropDown


This custom action allows creating a drop down menu whose contents reflect answers to previous questions or the result of a database query.

Create a drop down question no values, or with some values that will always be included in every dropdown, and note its name.
Then create a question of type Action, and fill it out as indicated here.

The Action question MUST follow the dropdown quesion,not precede it.

To fill a drop down menu using a SQL query

In the Action field, enter:
DynamicDropDown dropdown=name of the dropdown question to fill out

The Enter Script field contains a SQL statement, which can use question piping.
The SQL statement selects two fields, one for the values to be stored in the database and one for the visible contents of each option in the drop down menu.
If only one of those is available, select the column twice in the SQL query; note that some databases require using an AS clause after the repeated column.

Add datasource=JNDIDataSourcename right after DynamicDropDown to execute the SQL query using a different data source. Normally, DynamicDropDown finds tables in the same database that all of ViewsFlash uses. But sometimes, it is useful to find the data in a different schema or database altogether. In this case, create an additional JNDI Data Source and put its name in the datasource parameter. The additional database does NOT need to be the same database brand (eg., Oracle / DB2 ). More detail here.

Note that [/authenticateduserid] is the tag, described in question piping, that contains the user id of the current logged-in respondent.
Note that [/dbtablenameprefix] will use that paramer if set up in the servlet parameters to provide a prefix for the table name.

Examples:

1 To fill in a dropdown menu question named PickCountry from a table of two letter country codes and country names:
In the Action field, enter   
DynamicDropDown dropdown=PickCountry


In the Script field, enter:
select country_code, country_name from country_table
The table name is country_table, and the two fields in the table are are country_code and country_name.

2 To fill the same menu with only those countries in the continent selected by the respondent when answering an earlier question, use question piping:
In the Action field, enter    
DynamicDropDown dropdown=PickCountry


In the Script field, enter:
select country_code, country_name from country_table where country_continent=[/continent]
The table name is country_table, and the fields in the table are are country_code, country_name and country_continent.
The value entered in response to an earlier question, named "continent", is used in the where caluse to restrict the dropdown to only those rows where country_continent equals continent.
Note that [/authenticateduserid] is the tag, described in question piping, that contains the user id of the current logged-in respondent.
Note that [/dbtablenameprefix] will use that paramer if set up in the servlet parameters to provide a prefix for the table name.

To fill a drop down from values entered or selected in earlier questions

In the Action field, enter:
DynamicDropDown dropdown=name of the dropdown question to fill out,list of question names separated by commas

Examples:

1 A respondent is asked to enter the most important life goals using five text questions named goal1, goal2, goal3, goal4, goal5.
On the next page, a dropdown menu question named Goal is filled out from the responses to those five questions.

In the Action field, enter   
DynamicDropDown dropdown=Goal,goal1,goal2,goal3,goal4,goal5


2 A respondent is asked to enter the most important academic goals using three single-choice radio button questions named acad_goal1, acad_goal2 and acad_goal3
On the next page, a dropdown menu question named Acad_Goal is filled out from the responses to those three questions.

In the Action field, enter   
DynamicDropDown dropdown=Acad_Goal,acad_goal1,acad_goal2,acad_goal3


Note how this example is identical to the first, even though the question types used are very different.

3 A respondent is asked, using a multiple-choice checkbox question named languages_spoken, to indicate all the languages spoken at home.
On the next page, a dropdown menu question named primary_language is filled out from only those choices selected by the respondent.

In the Action field, enter   
DynamicDropDown dropdown=primary_language,languages_spoken

Security Note: If using User Security, the administrator must grant the questionnaire designer the right to use this custom action.

This Action is always executed before the page that contains the Action is composed.

It is also possible to create multiple-level drop down menus, such as car year, brand, model. See Cascading Menus.

Next: CascadingMenus