Have an idea?

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

Running code between begin and ends in one page

Hi there,

In a survey that i developed i have several free format fields that i'm setting a value based on answers in previous questions. Because these free format fields are affected by a previous question and affect the following question, the only way i can run the code that sets their values is by having a html filler page, between the two blocks of questions, that has a block of perl code.

Is there a way to make this block of code run on click of a control?

Thanks.

Carlos
asked Sep 2, 2015 by cdaniel (405 points)
Are you wanting to have code run in the browser before the page submits?
Or are you wanting code to run after the page has submitted the data to the server but before any skip logic, randomized blocks, or looping runs?
I'll try to exemplify:

In the first page i have a select question:

P1 - Text
Yes
No

After that i have a block of perl code:

if(VALUE("P1")==1)
{
    SETVALUE("P1_AUX",1)
}
else
{
    SETVALUE("P1_AUX",2)
}

And then i have a question that uses a constructed list based on the value of P1_AUX, and also has a different text based on the value of P1_AUX (i'm simplifying this to the max, as it's actually more complicated than that).

For this to work as is it now, these three steps need to be in separate pages, with the perl code in a page by itself.

1 Answer

+2 votes
So what you can do then is put your unverified perl code in a post skip on the page where P1 is answered.  Post skips run before any other logic. So here is what I would do.

Add a postskip to question P1.  Make sure its the first one as postskips run in sequential order.  As the skip to question just put always and any question you want.  It won't matter because we are not going to use it.
As the logic in the postskip put the following
Begin Unverified Perl
if(VALUE("P1")==1)
{
    SETVALUE("P1_AUX",1)
}
else
{
    SETVALUE("P1_AUX",2)
}
return 0;

End Unverified


The bottom line return 0 is important as it tells the post skip block to not skip to the question designated.  Post skip logic should evaluate to true or false.  1 = true, 0 = false.  But it doesn't matter what the code does because it will still run the code as long as the end result is a boolean value.

So what you should expect to happen is the respondent will answer P1.  P1 will be recorded, This setvalue code will run before questions future pages or questions are evaluated.  By doing this, everything should be in place for your constructed list.
answered Sep 2, 2015 by Mike Lodder Gold (23,410 points)
Mike, thank you. But if i try to put the logic in the skip, altough the syntax appears to be correct, when i try to run the survey i get an error #132.  I have the same exact code that i was running in an html filler question, so i'm not sure where the error comes from.
The code should be
Begin Unverified Perl
if(VALUE("P1")==1)
{
    SETVALUE("P1_AUX",1);
}
else
{
    SETVALUE("P1_AUX",2);
}
return 0;
 
End Unverified


Missing semi colons
Mike, thank you very much. It seems to work perfectly.
...