In a text input screen (type 5) I have a drop down input element (answer type 6) with several answer items. On leave OK I want to store the selected answer item's label in a local variable. Calling getAnswerData gives me the answer item's client key instead. How to get access to the label instead?
1 Answers
What you can do is to use getAnswerValue in the onLeaveOkPersistAssignment and allocate this value in a new local var e.g. $local:selectedItem. By doing so the clientKey will be stored in this var. On the next or follow up screen you can use this clientKey to access the right array pos value.
Example: Initially your question could look like this:
question key="q0" type="5" title="">
<answer key="q0a0" nextQuestionKey="q1" dummyAnswer="true" attributeType="6">
<item clientKey="0" dummyAnswerItem="true" />
<text/>
</answer>
<onEnterAssignment>
$local:myArray =
{
0:'Product 1';
1:'Product 2';
2:'Product 3';
};
addAnswer($answer:'q0a0', null, 'Dropdown List');
for(items:$local:myArray)
{
addAnswerItem($answer:'q0a0', null, '0', items, $local:myArray[items]);
}
</onEnterAssignment>
This example contains a dummyAnswer(key=q0a0) and a dummyAnswerItem, in the onEnterAssignment you are calling the answer in use of addAnswer() and allocate the answer items in use of addAnswerItem. Now in the onLeaveOkPersistAssignment of this answer you are using getAnswerValue(), in use of getAnswerValue you are assigning the clientKey to the local var.
<onLeaveOkPersistAssignment>
$local:selectedItem = getAnswerValue($answer:'q0a0');
</onLeaveOkPersistAssignment>
What you can now do is this on the same screen in use of changeEvents or on a follow up screen:
<question key="q1" type="0" title="">
<answer key="q1a0" nextQuestionKey="END" dummyAnswer="true"/>
<onEnterAssignment>
addAnswer($answer:'q1a0', null, $local:myArray[$local:selectedItem]);
</onEnterAssignment>
</question>
This allows you to print the label of the selected item on the next screen.

- 81
- 6
-
Two things that are still a bit unclear: 1.) Do the answer items require to be added dynamically? Is there also a way to handle this with static answer items? 2.) Is it advisable to call addAnswer with a clientKey param with the value null? – André Schäfer Apr 29 '15 at 07:58
-
1. it depends, in case that label and clientKey are the same you won't need to add them dynamically. If there are not it means you would have to. By trying to get access on the label in use of the getter methods the client key will always be returned, that's why I've used in my example the dynamic way. 2. you could define a clientKey val for sure but hence there is only one answer available I left it out. – lrs_coolik Apr 29 '15 at 09:23