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 answers from prior steps in the current survey using the Get Current Survey Answers method of the MyDataHelps.js SDK.

To find a previous step’s answer, 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:

Getting Current Survey Answers
MyDataHelps.getCurrentSurveyAnswers()
  .then( function(results) {
    var filteredResults = results.find( (answer) => answer.stepIdentifier === 'painScore' && answer.resultIdentifier === 'betterOrWorse');
    var answer = filteredResults.answers[0];
    console.log(answer);
  } );
MyDataHelps.getCurrentSurveyAnswers() .then( function(results) { var filteredResults = results.find( (answer) => answer.stepIdentifier === 'painScore' && answer.resultIdentifier === 'betterOrWorse'); var answer = filteredResults.answers[0]; console.log(answer); } );

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.

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.

Answer Format (Basic Steps)

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.

Sample Data
[
  {
      "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
  }  
]
[ { "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 } ]