Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Problem using SetValue where value doesn't display in Free Format Question

I have a free format question to collect contact information at the end of a survey. I am using merged fields to bring in that data if we already have it. My SetValue statements are similar to this:
[% SetValue(qRespInfo_City, GetValue('MergedCity')) %]

If I look at the back-end data, after I pass this script in the survey, I see that the City field is filled out, but when I get to that page in the survey, all the fields are blank. I tried adding a value directive like
value="[% qResp_City %]"
to each input field on the Free Format question, and the values then displayed, but then if I go forward a question and back to the info page, all fields are blank, even questions that don't have a value statement, and even though the Admin module is still showing data stored for all fields. If I backtrack through the survey to the place where I have the SetValue script and go forward again to the info page, the data shows up again.

Am I missing something obvious here, or can anyone help me shed some light on what's going on? Thanks!

asked Jun 20, 2014 by gtenney (170 points)
retagged Jun 20, 2014 by Walter Williams
The first thing I would do is check the sample fields are importing correctly. So before you decide on changing any sample fields with that final survey page, check the import has worked by displaying them early in the survey on one of the survey pages.

Then I would concentrate on editing one of the fields only on a test page. I'm not sure if you are using any Perl but be careful of case sensitivity as it is a common problem when programming Perl.

1 Answer

0 votes
It's a bit hard to say with just the snippits of code.  My first guess would be potentially a problem with the naming of the fields, meaning there is a mismatch between the HTML you have in the free format and how the variable name should exist in the database.

I'd probably try to break things down a bit to help identify where the problem is.  For example, I'd add a text/filler for experimenting.  First start with simply using [%VariableName%] and make sure you can reference something correctly.  Next add something like SetValue(VariableName,10) and make sure you can set the variable to a value of 10 by itself.  This step by step approach should help narrow down where the problem is.

If you are still stuck, you can zip up your project folder and mail it in to support@sawtoothsoftware.com
answered Jun 20, 2014 by Brian McEwan Gold Sawtooth Software, Inc. (45,995 points)
I have confirmed that the name and capitalization of both sets of fields is the same in both places. The snippet above was a shortened version I used for trying to resolve the issue, here is my original unverified Perl:

[% Begin Unverified Perl

my @getlist;
my @setlist;
my $i = 0;
my $retVal;

$getlist[0] = 'PersonFirstName';     $setlist[0] = 'qRespInfo_FirstName';
$getlist[1] = 'PersonMiddleName';     $setlist[1] = 'qRespInfo_MI';
$getlist[2] = 'PersonLastName';     $setlist[2] = 'qRespInfo_LastName';

for ($i = 0; $i < $#getlist + 1; $i++) {
    $retVal .= $getlist[$i] . ': ' . GETVALUE($getlist[$i]) . '<br />';
    SETVALUE($setlist[$i], GETVALUE($getlist[$i]));

return $retVal;

End Unverified %]

I added the $retVal variable to confirm the GETVALUE was working, and it is. At this point in the survey, the script returns the first, middle, and last name correctly. If I look at the data in the admin module, the first, middle, and last name show up there correctly.

When I get to the free format page all fields are blank (there are more fields, I am just using the first 3 for now.) If I enter a new first, middle, and last name, then look at the admin module, the newly typed name overwrote the value I set using SETVALUE.
I'm not really a programmer, so I can't say for certain if your code should work.  But I like Paul's example, as it speaks to how to approach debugging in general.  Don't be afraid to toss in a text/filler page in between questions where you reference previous values on the page to make sure your code behind the scenes is going to work correctly.  

Also in re-reading the question, it sounds like you are trying to use SetValue to populate a text box on the page.  SetValue is for interacting with the database when a page submits, so it wouldn't push the value of variable onto the page inside a text box.  

The part that should work would be doing something like value=[%VarName%] in the HTML.  If that's not working, then you probably don't have the correct variable name.  Toss the same [%VarName%] code into the header of a question so it displays properly, again to try to debug what is going on under the hood of code you can't see when the survey is running.
Thanks, Brian. I did try a lot of debugging steps to help get this working, that's always good advice! I was actually planning on using SetValue at the beginning of the survey so that all the information would be there for the respondent when they go to the last page, but even though it showed up in the data set before I got to that page, it would just show up on that page.

In any case, Sawtooth Support suggested the value=[%MergeVarName%] and even though I suspected it would always overwrite the new value (if changed) with the original merged value, it didn't. So it was odd and unexpected but seemed to work. Thanks, Brian!