Deep dive: Generate repeatable groups from associated records
| 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 a custom JavaScript function can be used to generate as many repeatable groups as there are associated records of the parent. This use case showcases a scenario where we open a questionnaire directly from a work order, with as many repeatable groups as there are associated work order incidents. These repeatable groups are automatically generated based on the number of related incidents. Each group contains a lookup question filled with an incident.
Prerequisites
For this script to work, your questionnaire must contain the following:
- Workorder lookup question.
- Repeatable group with workorder lookup incident question.
Download sample files
For your convenience, we are providing you sample file: [script]
Script
This script is explicitly built for workorder and workorder incidents. If you wish to use the script in another scenario, redefine these variables and modify the fetches.
const CHECKLIST_STEP_GROUP_NAME = "group";
const WO_INCIDENT_ENTITY_NAME = "msdyn_workorderincident";
const WO_INCIDENT_LOOKUP_QUESTION_NAME = "work-order-incident";
const WORKORDER_LOOKUP_QUESTION_NAME = "resco_workorder";
Execution order
These are the main functions used in the script:
- generateQuestionnaireContent
- loads the questionnaire context
- Initializes loadTemplateGroupId and generates groups
- loadTemplateGroupId
- finds the ID of the group we want to repeat
- generateGroups
- Initializes LoadWorkorderIncidents and repeatNext, by invoking repeatGroup, we already have two instances of group, therefore we subtract 2.
- LoadWorkorderIncident
- Fetches and returns dynamic entities with incidents.
- repeatNext
- generates as many repeatable groups as there are incidents
- initializes setGroupValues.
- setGroupValues
- fills in the questions from the incident into already created repeatable groups.