Skip to main content
Single/Multiple answer values in lua
Justin Rohrer avatar
Written by Justin Rohrer
Updated this week

VerticalChange now allows you to write lua scripts that reference single or multiple select field values in addition to the field slugs. These data can be accessed from answers.<field_slug>.VALUES. The VALUES field is an array containing the value, slug, and additional text information of any selected option(s).

Single answer fields

This table below presents example lua code and the output it will provide for the field pictured. For the purposes of this example, the field is slugged single_answer and the three options are slugged first_option, second_option, and third_option respectively.

Lua code

Result

answers.single_answer.first_option

true

answers.single_answer.second_option

false

answers.single_answer.VALUES[1].value

"First Option"

answers.single_answer.VALUES[1].slug

"first_option"

answers.single_answer.VALUES[1].tc

"UMMMMMMMM"

Multiple answer fields

These fields are more complex than their single select counterparts because there may be multiple entries in VALUES. The first checked value (Note that un-selected values are skipped) can be accessed by calling VALUES[1], just like the single-answer field. The second, third, and any additional checked options may be accessed from VALUES[2], VALUES[3], through VALUES[n]. Un-checked values will not have a corresponding VALUES[] entry. An example is presented below. For the purposes of this example, the field is slugged multiple_answer, and the three options are slugged first_option, second_option, and third_option, respectively. The table shows the code and resultant values rendered by that code.

Lua code

Result

answers.multiple_answer.first_option

true

answers.multiple_answer.third_option

false

answers.multiple_answer.VALUES[1].value

"First Option"

answers.multiple_answer.VALUES[1].slug

"first_option"

answers.multiple_answer.VALUES[1].tc

""

answers.multiple_answer.VALUES[2].value

"Second Option"

answers.multiple_answer.VALUES[2].slug

"second_option"

answers.multiple_answer.VALUES[2].tc

"UMMM"

answers.multiple_answer.VALUES[3].value

ERROR

answers.multiple_answer.VALUES[3].slug

ERROR

answers.multiple_answer.VALUES[3].tc

ERROR

Note the errors caused by entering a number between the brackets higher than the number of checked options. In order to access multiple-answer values data, it is therefore necessary to use a control structure such as a for loop or an if/else statement. Some examples follow.

Accessing a specific option's text entry field

The code below scans to see if the item with the slug "second_option" has been checked, then, if it has, it saves that text entry data to a local variable named out, which can then be written to another field.

out = ""
for i=1, table.getn( answers.multiple_answer.VALUES ) do
val = answers.multiple_answer.VALUES[i]
if val.slug == "second_option"
then out = val.tc
end
end

Collecting all checked values

The code below pulls together all checked option labels and saves them to a local variable named out as a comma-separated list, which can then be written to another field or used elsewhere

out = ""
for i=1, table.getn( answers.multiple_answer.VALUES ) do
val = answers.multiple_answer.VALUES[i]
out = out .. val.value .. ", "
end

Collecting all checked values and any associated text entry data

The code below pulls together all checked option labels and any additional text and saves them to a local variable named out as a comma-separated list, which can then be written to another field or used elsewhere

out = ""
for i=1, table.getn( answers.multiple_answer.VALUES ) do
val = answers.multiple_answer.VALUES[i]
out = out .. val.value .. (val.tc or '').. ", "
end
Did this answer your question?