Accessing Previous Step Results

Within a Web View step, you may want to access the results of previous steps. This is done via window.taskResult.

Task Results

The task result object stores a variety of information about the survey response, including the device information and survey task identifier. You’ll generally only care about the StepResults field, where all the results of previous steps are stored.

Step Results

StepResults is an array of results, each containing the following fields:

Field Description
Identifier The step identifier.
StartDate Date the step was started.
EndDate Date the step was completed.
QuestionResults Detailed results - see below for more information.

Question Results

The contents of the QuestionResults step varies by step type, but there are some fields in common.

Field Description
Identifier Identifies the question. This may be the step identifier or a form question identifier.
Type Type of question, either “QuestionResult” or “WebViewStepResult”.
StartDate Usually the same as the StepResult start date, but some devices have more fine-tuned input tracking.
EndDate Usually as the StepResult end date, but some devices have more fine-tuned input tracking.
Answer, Choices, or Result The actual answer will be in one of these fields depending on the question type, as explained below.

Instructions

For display-only steps like instructions, QuestionResults is an empty array.

Simple Questions

For simple steps like text or number entry, QuestionResults is an array with a single entry. The question result Identifier corresponds to the step identifier, and the answer is stored in the Answer field. For example:

[
  {
    Identifier: "Sleep Hours",
    ...
    Answer: "2"
  }
]

Choice Questions

For choice questions (text choice or yes/no choice), QuestionResults is a single entry. The question result Identifier corresponds to the step identifier, and the selected answers are stored in the Choices field. For example:

[
  {
    Identifier: "Symptoms",
    ...
    Choices: [
      "Headaches",
      "Nausea"
    ]
  }
]

Form Steps

For form steps, QuestionResults is an array with one entry for each item on the form. The question result Identifier corresponds to each form item’s individual identifier. The answer is in either the Answer or Choices field, as described above, depending on the type of each form item.

[
  {
    Identifier: "form 1",
    ...
    Choices: [
      "Headaches",
      "Nausea"
    ]
  },
  {
    Identifier: "form 2",
    ...
    Answer: "2"
  }
]

Web View Steps

For web view steps, QuestionResults is an array with a single entry. The question result Identifier corresponds to the step identifier, and the answer is in the Result field.

[
  {
    Identifier: "Web Example",
    ...
    Answer: "Right Leg"
  }
]

Example

Below is a simple example that retrieves an answer from a question step. For a more robust example, you can import the survey named Web View Step: Accessing Previous Survey Results from the Survey Store and examine its code.

function getTextAnswer(stepIdentifier) {
    var stepResult = window.taskResult.StepResults.find(function(stepResult) {
      return stepResult.Identifier === stepIdentifier;
    });
    if ( !stepResult ) return null;
    var questionResult = stepResult.QuestionResults[0];
    return questionResult.Answer;
  }