Chapter 12 – Advanced Formulas. Count Employees with Low Mood Scores

In this advanced lesson, we are going to use the Formula tab to write custom business logic in JavaScript. The goal is to identify how many employees have a Mood Score of 4 or below. This will help us quickly spot employees with low satisfaction.

5 min

Step 1 - Create a new Formula

Open the Formulas tab in your project and create a new Formula.

Switch the editor to JavaScript Advanced Mode by toggling the mode button. This allows us to write custom code directly.

Step 2 - Write the business logic

Here is the JavaScript code we will use in this formula:

(async () => {
 const rows = fields["grid1"] ?? [];
 // Filter all rows where Mood Score <= 4
 const lowMoods = rows.filter(r => {
   const score = Number(r["Mood Score"]?.value ?? 0);
   return score <= 4;
 });
 const count = lowMoods.length;
 if (count === 0) {
   return "All employees have a Mood Score above 4.";
 }
 return `${count} employee(s) have a Mood Score of 4 or below`;
})();

This script goes through all rows of the DataGrid named grid1, checks the Mood Score column, and counts how many employees have a score of 4 or lower.

Step 3 - Configure the DataGrid correctly

By default, the DataGrid is set to return only the selected row. In this case, we want to analyze all rows in the table.

To fix this:

  1. Open the DataGrid settings.
  2. Switch to Advanced Mode.
  3. In the “Return value on selection” option, change it from Selected row to All data.
  4. Validate your settings.

Step 4 - Display the result

Go back to your page and drag a Description component.

In its configuration, set the value to the Formula you just created.

Now go to Preview mode:

  • If all employees have a score above 4, you will see the message:
  • “All employees have a Mood Score above 4.”
  • If not, you will see:
  • “X employee(s) have a Mood Score of 4 or below.”

With this example, you’ve learned how to:

  • Use the Formula tab in advanced JavaScript mode,
  • Access all rows of a DataGrid instead of just the selected one,
  • Write custom business logic to analyze your data dynamically.