Deep dive: Create records from repeatable groups

From Resco's Wiki
Jump to navigation Jump to search
Warning Work in progress! We are in the process of updating the information on this page. Subject to change.

In this guide, we will demonstrate how custom JavaScript can create records from repeatable group instances in Questionnaire. This use case showcases the creation of Incidents. Each repeatable group instance includes an Asset lookup and a short-text question. In the newly created Incident, we want to see the parent Account, Questionnaire where it originated, associated Asset, and the short-text question as a title.

Execution

This is how the script execution looks like in the app:


Prerequisites

For this script to function properly, the following conditions must be met:

  • Each question from the repeatable group you want to save into a record must exist as a field in the destination table (Asset lookup question --> Asset lookup field in the Incident).
  • Be wary of server logic when creating new records. All required fields must be filled in. Otherwise, the sync will fail, and records won't be uploaded.

Download sample files

For your convenience, we are providing you sample file:
File:CreateFromRepeatableGroups.zip - JavaScript code

Set up your app project

In this section, we add the JavaScript code to your app project.

  1. Start Woodford and edit the app project used for opening the questionnaire.
  2. Go to Components > Offline HTML or create dedicated folder for questionnaire scripts.
  3. Upload the attached file [script].html or use New File template and paste the script above inside the <script></script> tags. If you already have an HTML file for your questionnaire, just add the function to that file.
    adding js code to your project
  4. Verify that the Offline HTML folder also includes an updated version of JSBridge.js (used for facilitating the communication between our custom script and the app). Also, the uploaded script must link that file. See Including the JSBridge.js file for detailed instructions.
  5. Save all changes and publish the app project.

Set up the questionnaire

Finally, we need to add the script reference to the questionnaire.

  1. Start Questionnaire Designer.
  2. Click Import and select the downloaded questionnaire.
  3. Edit the questionnaire and verify the following settings. If you are using your own, custom questionnaire, use these as inspiration for recreating the needed functionality.
  4. Go to questionnaire Options and on the Rules tab, set Script Path to createFromRepeatableGroups.html (if the script is directly in root) or file://Questionnaire/createFromRepeatableGroups.html (if in subfolder).
    enter script path in questionnaire