Lifecycle
- Choose a form element linked to an ActivityDefinition
- Collect inputs via the ActivityDefinition’s UI
- Run the ActivityDefinition’s
calculatefunction on the server (on submit) - Persist the result as an Activity and link it to a FormElementSubmission
- View, export, or recalculate as needed
Data Model
Each Activity contains:definitionId: the ActivityDefinition usedinput: the submitted values (JSON)value: numeric result of the calculationmetadata: calculation details (JSON)- linkage to form submission, form element, site, year, period
Working With Activities
Activities are managed via the Activities router (with REST mirrors):-
activities.addActivity- Input:
{ formId, key, siteId, year, periodUnit, input } - Resolves the form element by
formId + key, runs the linked ActivityDefinition’s calculation, saves the result.
- Input:
-
activities.getActivities- Input:
{ formId, key, siteIds?, years?, periodUnits? } - Returns activities with parsed
input/metadataand context (siteId,siteName,year,periodUnit).
- Input:
-
activities.importActivities- Bulk import; all rows must target elements linked to the same ActivityDefinition; runs per-row calculation with detailed error reporting.
-
activities.exportActivities- CSV/XLSX export with consistent columns (see below).
-
activities.removeActivity- Deletes an activity and its FormElementSubmission rows; applies workflow rules.
-
activities.recalculateActivity/activities.recalculateActivities- Re-runs calculations using the current ActivityDefinition
calcCodefor a single or multiple activities.
- Re-runs calculations using the current ActivityDefinition
$datasets/$kpis/$sites helpers in calculations use server-side RLS for the current organization.
Export Format
Exports include these columns in order:activityId,site,year,periodUnit,value- Each input from the ActivityDefinition’s
inputsarray (as columns matching their names) metadata_*columns for each returned metadata key
metadata to be included.
Example columns:
- Context:
activityId,site,year,periodUnit,value - Inputs:
electricityKwh,gridRegion, … (from definition) - Metadata:
metadata_factor,metadata_year, … (flattened keys)
Import Rules & Errors
- All rows must reference form elements (via
key) that share the same ActivityDefinition. - Site access is validated per row.
- Errors are returned with row number and context (key/site/year/period) and a user-friendly message extracted from the calculation, e.g.:
Row 3 (energy - siteA 2024 Period 1): electricityKwh must be a non-negative number.
Recalculation
- Recalculation uses
- the current ActivityDefinition
calcCode, - the stored
input, and - current context (site/year/period) and datasets.
- the current ActivityDefinition
- NaN/Infinity are normalized to
0in bothvalueand numericmetadataentries.
UI & Spreadsheet
- The ActivityDefinition’s
uiCodeis rendered dynamically and callsonAddActivity(values)on submit. - The spreadsheet view lists activities for the element with filters for site/year/period, showing input and metadata fields.
Workflow & Status
- If the configured workflow requires approval, any add/remove/recalculate/import in a period marks all submissions in that period as
COMPLETED(pending approval). - Otherwise, submissions remain
APPROVED.
Dataset Lookup Example
Error Handling & Quality
- Throw clear
Errormessages in the calculation for invalid inputs. - Keep input names clean and consistent with the definition.
- Use
examplevalues in inputs to improve CSV detection and previews.
Security
- Site access is enforced on all operations.
$datasets/$kpis/$sitesare server-provided helpers subject to org-level RLS.
See Also
- ActivityDefinition (authoring inputs/UI/calc and preview)
- Activity Calculation Variables (context helpers and examples)
- Dataset Access Examples (end-to-end patterns)
- API Examples
YOUR_TOKEN and IDs):
List activities
Troubleshooting
- Add/import fails with a user error → The calculation likely threw
Error("..."); fix inputs or calc logic per the message. - Import reports multiple row errors → Use the row index and context in the error details to correct data.
- Recalc produced zeros → Check dataset values/keys for the target year; ensure fallbacks are in place.
- Export missing input column → Ensure the field exists in the ActivityDefinition
inputsschema.
Performance Tips
- Avoid repeated dataset lookups when a single
getCoefficientssuffices. - Keep per-activity metadata small for faster exports.
Metadata Conventions
- Prefer explicit keys and units (e.g.,
efKgCO2ePerKWh). - Include
year,period, orsiteIdwhen it aids downstream analysis.

