Calculation of data fields
How to configure calculation using dynamic expressions
With expressions you can automatically calculate the value of data model fields. The expression engine reads the rules from a JSON Schema specification.
How to configure calculations with expressions
Calculations with expressions are defined in a separate file next to your data model, using the naming convention name.calculation.json.
If your data model is called skjema, you will already have files like skjema.cs and skjema.schema.json. The file you create should be in the same folder and be called skjema.calculation.json.
You can copy the content below as a starting point:
template.calculation.json{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json",
"calculations": {}
}
Define a calculation rule
Below you can see an example of a calculation of the field regnskap.sum in the data model:
example.calculation.json{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json",
"calculations": {
"regnskap.sum": {
"expression":
["minus",
["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
}
}
}
The rules for the fields in the data model are set in the calculations object, where the data model path is the key and the value is a rule.
Unlike validation using expressions, calculations using expressions do not support lists.
A rule consists of an expression, which is a dynamic expression that returns a number, boolean, or string value. See dynamic expressions for more information.