Accessing Previous Step Results
Within a Web View step, you may want to use the results of previous steps to inform the behavior of the current step. For example, you may need to calculate and display results based on prior answers.
Getting Current Survey Answers
You can get information about the current survey using the Get Current Survey Answers method of the MyDataHelps.js SDK. You will need both the step and result identifier. The stepIdentifier
is set in the survey designer. The resultIdentifier
will refer to either the form item identifier (for form steps) or the step identifier (for non-form steps).
For example, if painScore
is a form step and betterOrWorse
is the identifier for one of the items on the form, you can get that answer like so:
MyDataHelps.getCurrentSurveyAnswers()
.then( function(results) {
var answer = results.find( (answer) => answer.stepIdentifier === 'painScore' && answer.resultIdentifier === 'betterOrWorse');
console.log(answer);
} );
This requires your Web View steps to include the MyDataHelps.js SDK. See the JS SDK Getting Started page for more details.
The getCurrentSurveyAnswers()
returns a list of step answer data: one entry for each Question and Web View step, and one entry for each of the questions within a Form step.
Note
Only Question, Form, Consent, and Web View steps are included in the output.
Each entry includes the following fields:
Field |
Description |
date |
Date/time of when the question was answered. |
stepIdentifier |
The step identifier from the survey designer. |
resultIdentifier |
This will usually be the same as the step identifier, but for form steps it will be the identifier of the specific form question. |
answers |
Depends on field type - see below. |
For Question, Form, and Web View steps, the answers
field is a collection of strings representing the step answers. A few notes about the format:
- Answers is always an array, even if there is just a single answer.
- For a choice step, answers will include the participant’s selected choice(s).
- Answers are always stored in string format. Dates are stored a “YYYY-MM-DD” and yes/no questions as “Yes” and “No”.
- Heights and weights are always represented in cm/kg, regardless of the units presented to the participant.
For Consent steps, the answers
field is an empty array and there are three additional fields capturing the consent details:
givenName
- The participant’s given name.
familyName
- The participant’s family name.
consented
- A boolean value indicating whether the participant consented.
Sample Data
Below is sample output from getCurrentSurveyAnswers()
.
The first step is a Question step, so the step and result identifiers are the same and there is a single answer.
The second step is a Form step with two questions. Thus there are two entries with the same step identifier and different result identifiers.
[
{
"date": "2021-02-11T09:32:50.205-05:00",
"stepIdentifier": "MoodRating",
"resultIdentifier": "MoodRating",
"answers": [
"7"
]
},
{
"date": "2021-02-11T09:32:55.205-05:00",
"stepIdentifier": "Form1",
"resultIdentifier": "Question1",
"answers": [
"A", "B"
]
},
{
"date": "2021-02-11T09:32:55.205-05:00",
"stepIdentifier": "Form1",
"resultIdentifier": "Question2",
"answers": [
"2022-10-20"
]
},
{
"date": "2021-02-11T09:32:57.205-05:00",
"stepIdentifier": "Consent1",
"resultIdentifier": "Consent1",
"answers": [],
"familyName": "Smith",
"givenName": "John",
"consented": true
}
]