Deep dive: Aggregate questions in repeating groups

From Resco's Wiki
Jump to navigation Jump to search

In this guide, we will demonstrate how a custom reusable JavaScript function can be used from Resco rules to add new functionality to Resco questionnaires. This show the pattern how developers can build libraries of reusable functions that can be used from low-code rules by functional consultants or business users to enrich questionnaires with complex behavior.

The example shows how rules and custom JavaScript code can work together to calculate total value of questions inside repeating group of a Resco questionnaire.

Download sample files

For your convenience, we are providing you two sample files:

You can either use the samples above (in later sections we explain how to add them to your organization), or you can create them using our instructions (also explained below).

About the script

Let's start with custom JavaScript function that takes all instances of question in a question group and returns total of its values:

// Returns total sum of values of the question in a repeatable group.
// The following shared variables must be set:
// groupTemplateName - the template name of repeatable group
// aggregatedQuestionName - the name of question to be summed.

async function calculateTotal() {
    return new Promise(function(resolve, reject) {
        MobileCRM.UI.QuestionnaireForm.requestObject(function(form) {
            const groupTemplateName = form.questionnaire.properties.groupTemplateName;
            const aggregatedQuestionName = form.questionnaire.properties.aggregatedQuestionName;
            let sum = 0;                
            const groupIds = form.groups.filter(group => group.name.startsWith(groupTemplateName)).map(group => group.id);
            if (groupIds) {
                const vals = form.questions.filter(q => q.name.startsWith(aggregatedQuestionName) && groupIds.includes(q.groupId)).map(q => q.value ?? 0).forEach(v => sum += v);
            }
            resolve(sum);                  
        }, MobileCRM.bridge.alert);
    });
}

A few points to note:

  • Summed question must be of numeric type (Whole or Decimal number)
  • Promise object pattern must be used to ensure that result of calculation is returned to the rule via call to resolve() function. This pattern must always be used if we need the function to return value to the calling rule.
  • The function is generic and can be used in any questionnaire, but it needs to know with which question in which group to work with. Since current version of Resco Suite doesn't allow passing parameters to JavaScript functions from rules, the function relies on these parameters to be present in shared variables groupTemplateName and aggregatedQuestionName. Calling rule must fill these variables before calling the function.

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.
  3. Upload the attached file AggregateRepeatingGroup_v3.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 prepare the questionnaire. To demonstrate how to call custom function from questionnaires, see how it is done in the attached sample template Aggregated Repeated Group v3.zip. Alternatively, you can do the same to reuse the function in your questionnaire: just adjust the values of shared variables to the names of your repeating group and aggregated question.

  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.
    • Go to questionnaire Options and on the Rules tab, set Script Path to "AggregateRepeatingGroup_v3.html".
      enter script path
    • Go to the On Load rule, and declare two shared variables. They will be used as parameters to the function call.
      declare shared variables
    • Select the repeatable group and in its On Change rule, enter code that reacts to the user changing product price, and invokes calculation. The rule uses shared variables to pass group name and question name as parameters to the JavaScript function.
      on change rule for the questionnaire group
    • There is one problem left to resolve. Deleting the instance of question group doesn’t trigger On Change. Use the group's On Delete rule which sets the price of deleted question to zero. This triggers the On Change rule, causing the total to be recalculated:
      on group delete set price to zero

This is it. Your questionnaire will now automatically calculate total prices of added products.