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.
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.
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.
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:
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:
With this example, you’ve learned how to: