The exec element is for advanced users with programming experience. It allows you to run Python code from your survey. Respondents will not see exec elements.
1: Adding an Exec Element
To insert an exec element, add a new survey element, select "Structural" and choose "Exec". Then click "Insert".
Add Python code in the space provided. For more information on Python, click here.
2: General Options for an Exec Element
The options available are located on the right side of the screen. Each of the following options, when altered, affect all attributes within the exec element.
- Show If: Allows you to create new logic conditions or select an existing logic condition for the element. For more information, click here.
- When: Allows you to control when the exec element should be executed. Click here for more information and examples of the following options.
- finished: The exec element will execute once just after the respondent has submitted the survey and before the results are written to the disk. This is often used to add final data to a database and record any remaining data you may want to store (e.g. total time the respondent spent taking the survey, IP address, etc...).
- finishedPost: The exec element will execute after the respondent has submitted the survey and all data has been saved (including state-data, timestamp, and extraVariables). This code will be run by the survey process, so heavy processes should be avoided.
- flow: The exec element will execute when "One Page" mode has been toggled.
- init: The exec element is executed once and only once when the survey is loaded. There is no respondent information at this point, so you cannot access extra variables nor can you modify or access question content.
- returning: The exec element will execute when the respondent is returning after being redirected using a suspended redirect.
- started: The exec element is executed as soon as the respondent enters the survey.
- submit: The exec element will execute each time the "Continue"/"Finish" button is pressed and after a <validate> runs within a question element.
- survey: This is the default setting for the when attribute. By default, the <exec> tag will execute just like any other question, when the respondent reaches its location within the survey.
- verified: The exec element will execute each time the respondent has successfully submitted verified data. This is helpful if you're making calls to an external API each time a question has been answered properly.
- virtual: The exec element will run once for each respondent when calculating <virtual> questions.
- virtuallnit: The exec element is executed when running reports and is used to populate virtual questions. Similar to when="init", when="virtualInit" code will be usable for virtual questions across your entire survey.
- More Options: Use the dropdown menu to select less commonly used options. Once selected, you will be able to specify the setting for that option. For more information on each option, click here.
- Raw Options: If you prefer to manually enter the XML options, use the "raw options" dialogue. This is useful for copying and pasting options between survey elements.