Initialize Expressions

The initilization feature allows survey builders to create code that will pre-populate a question based on the answer to a previous question. Some of the applications include: - Prepopulate a column in a matrix based on a previous checklist question
- Prepopulate a map question location or area based on an API
- Prepopulate a farmOS field name in a matrix question based on the field specified at the top (participating fields for example)
- Prepopulate a number score based on some calculation from previous questions
- Prepopulate text based on a unique combination of things - for example, auto-generate field names for people so they don't have to make them themselves.

Watch this video to see the initialization feature in action or look at some of the use cases below:

Use Case 1: Prepopulate a column in a matrix based on a previous checklist question. This example is taken from the Regen1 onboarding survey.

let exclude = ['shallow','less_frequent','equipment','maintain_residue','disk','cultivator'];

function initialize(submission, survey, parent) {
  let practices = utils.getCleanArray(parent.details);
  practices = practices.filter(item => !exclude.includes(item));  // remove practices we don't want... 
  let populated_matrix = [];
  practices.forEach((practice) => populated_matrix.push({ acres: { value: null}, practice: {value: [practice]}, verification: { value: null } }));
  console.log('populated_matrix');
  console.log(JSON.stringify(populated_matrix));

  return populated_matrix;
}

Where details is the data name of the question being referenced and practices is an array variable being created to store the selected answer values from the reference question. Acres/practice/verification are the data names of the columns in the matrix question we want to push to (this should be created before writing your initialization code). In this case we want to pre-populate only the practice column in the matrix so we will push null to the other columns so the user can fill them in themselves.

Use Case 2: Prepopulate a column in a matrix based on an online resource. Note that you need to access something which does not have CORS restrictions. Files in gitlab are CORS restricted, however, artifacts or outputs to gitlab pages are not.

async function initialize(submission, survey, parent) {

    let url = 'https://our-sci.gitlab.io/-/software/json_schema_distribution/-/jobs/4747806888/artifacts/collection/conventions/log--activity--flaming/schema.json';

    const data = await fetch(url).then(function (response) {
       return response.text();
   });

    let jsonData = JSON.parse(data);

    let objects = Object.keys(jsonData.properties)
    let descriptions = objects.map((a) => jsonData.properties[a].description);

    let output = [];
    objects.forEach((key, index) => {
      output.push({
        issues: {
          value: [`${key}`]
        },
        description: {
          value: `${descriptions[index]}`
        }
      })
    })

    return output
}

Use Case 2: Prepopulate a text question with a script output.

function initialize(submission, survey, parent) {
  let val = JSON.stringify(parent.script_1.value);
  return [val];
}