Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Calculation of data fields using expressions
description: How to configure calculation using dynamic expressions
toc: true
---

Calculation of data fields using expressions makes it possible
to set data model fields with the expression engine through a JSON
schema spec.

## How to configure calculation with expressions

{{% notice info %}}
Calculation with expressions cannot be configured through Altinn Studio Designer at present.
{{% /notice %}}

Calculation with expressions is 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`, and the file you create should be in the same folder and called `skjema.calculation.json`.
You can copy the content below as a starting point:

{{< code-title >}}
template.calculation.json
{{< /code-title >}}
```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:

{{< code-title >}}
example.calculation.json
{{< /code-title >}}
```json
{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json",
"calculations": {
"regnskap.sum": [
{
"condition":
["minus",
["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The example should use dataModel consistently.

The first lookup uses datamodel, while the second uses dataModel. If dataModel is the actual function name, this example will fail when copied as written.

Suggested fix
         "condition":
           ["minus",
-            ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
+            ["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
           ]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"condition":
["minus",
["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
"condition":
["minus",
["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.en.md`
around lines 44 - 47, The example uses inconsistent function casing —
"datamodel" vs "dataModel" — which will break; update the example so both
lookups use the actual function name "dataModel" (replace "datamodel" with
"dataModel") in the "condition" expression so the calls to
dataModel("regnskap.inntekter") and dataModel("regnskap.utgifter") are
consistent.

}
]
}
}
```

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 list of rules.

Unlike validation using expressions, calculations using expressions do not support lists.

A rule consists of a **condition**, which is a dynamic expression that returns any object type. See [dynamic expressions](/altinn-studio/v8/reference/logic/expressions/) for more information.

Check failure on line 58 in content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.en.md

View workflow job for this annotation

GitHub Actions / Build and Check links

bad links: altinn-studio/v8/reference/logic/expressions
Comment on lines +54 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

any object type is too broad here.

This reads as if calculation expressions can return arbitrary object values, but the linked expressions docs describe a constrained set of supported value types. Please document the supported/expected return types here instead of “any object type”.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.en.md`
around lines 54 - 58, The phrase "any object type" in the description of a
calculation rule's condition is too vague—update the text that describes the
return value of the condition in the `calculations` documentation to enumerate
the supported return types from the dynamic expressions system (e.g., boolean,
number, string, null, object, and array) and briefly note any constraints (for
example: objects/arrays must be serializable and expressions cannot return
functions), referencing the `calculations` object and the `condition` concept
and keeping the link to the dynamic expressions docs for details.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Kalkulering av datafelt ved hjelp av uttrykk
description: Hvordan konfigurere kalkulering ved hjelp av dynamiske uttrykk
toc: true
---

Kalkulering av datafelt ved hjelp av uttrykk gjør det mulig
å sette datamodellfelt med uttryksmotoren gjennom en json
schema spec.

## Hvordan konfigurere kalkulering med uttrykk

{{% notice info %}}
Kalkulering med uttrykk kan ikke konfigureres igjennom Altinn Studio Designer per nå.
{{% /notice %}}

Kalkulering med uttryk defineres i en egen fil ved siden av datamodellen din, og bruker navne-konvensjonen `navn.calculation.json`.
Hvis datamodellen din heter `skjema` skal du blant annet ha filene `skjema.cs` og `skjema.schema.json` fra før, og da skal filen du oppretter ligge i samme mappe, og hete `skjema.calculation.json`.
Comment on lines +7 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Polish the Bokmål in the intro before publishing.

This section still has a few visible language issues (json schema spec, uttryk, navne-konvensjonen), so the page reads unfinished in nb-NO.

As per coding guidelines, **/*.nb.md: Check for Norwegian Bokmål (nb-NO) grammar and spelling with extra care.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.nb.md`
around lines 7 - 18, Polish the Bokmål in the intro: fix typos and improve
phrasing (e.g., change "uttryk" to "uttrykk", "navne-konvensjonen" to
"navnekonvensjonen", and replace "json schema spec" with a proper term like
"JSON Schema-spesifikasjon"), and rework sentences for smooth Norwegian (for
example rephrase the first line to something like "Kalkulering av datafelt ved
hjelp av uttrykk gjør det mulig å sette felt i datamodellen ved hjelp av en JSON
Schema-spesifikasjon."), and ensure the examples `navn.calculation.json` and
`skjema.calculation.json` remain clear and consistent with the surrounding text.

Du kan kopiere innholdet nedenfor som et utgangspunkt:

{{< code-title >}}
template.calculation.json
{{< /code-title >}}
```json
{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json",
"calculations": {}
}
```

### Definere en kalkuleringsregel

Nedenfor kan du se et eksempel på en kalkulering av feltet `regnskap.sum` i datamodellen:

{{< code-title >}}
example.calculation.json
{{< /code-title >}}
```json
{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json",
"calculations": {
"regnskap.sum": [
{
"condition":
["minus",
["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The example uses the wrong expression function name.

Line 46 uses datamodel, but the documented function name elsewhere is dataModel. Readers copying this example will get a non-working expression.

Suggested fix
         "condition": 
           ["minus", 
-            ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
+            ["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
           ]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"condition":
["minus",
["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
"condition":
["minus",
["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.nb.md`
around lines 44 - 47, The example's expression uses the incorrect function name
"datamodel" inside the "condition" array (the element ["datamodel",
"regnskap.inntekter"]) which should match the documented "dataModel" function;
update that element to use "dataModel" so both operands use the same correct
function name (e.g., replace ["datamodel", "regnskap.inntekter"] with
["dataModel", "regnskap.inntekter"] in the "condition" array alongside the
existing ["dataModel", "regnskap.utgifter"]).

}
]
}
}
```

Reglene for feltene i datamodellen settes i `calculations`-objektet, hvor datamodell-stien er nøkkelen, og verdien er en liste med regler.

I motsetning til validering ved hjelp av uttrykk, støtter ikke kalkuleringer ved hjelp av uttrykk lister.

En regel består av en **condition**, som er et dynamisk uttrykk som returnerer en hvilke som helst objekttype. Se [dynamiske uttrykk](/nb/altinn-studio/v8/reference/logic/expressions/) for mer informasjon.
Comment on lines +54 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Please narrow the return-type description here.

returnerer en hvilke som helst objekttype looks incorrect for the expression engine. The expressions reference documents supported datatypes, and object/list values are not generally supported results to work with here. This should describe a supported value type instead of “any object type”.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@content/altinn-studio/v8/reference/logic/dataprocessing/calculation/_index.nb.md`
around lines 54 - 58, The description for calculation rules is too vague:
replace "returnerer en hvilke som helst objekttype" with a narrow list of
supported expression return types and state the expected type for a rule
condition — e.g., clarify in the `calculations` documentation that a rule's
`condition` (see `dynamiske uttrykk`) must evaluate to a boolean (or other
supported scalar types such as number or string where relevant), and that
complex object/list values are not valid return types for these expressions.

Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ gir dette uttrykket en feilmelding.

## Datatyper

Funksjoner i uttrykkene har en forventning om at argumentene som blir sendt inn har en spefikk type. Dersom et argument
Funksjoner i uttrykkene har en forventning om at argumentene som blir sendt inn har en spesifikk type. Dersom et argument
blir sendt inn har en annen type enn forventet, blir verdien forsøkt konvertert til riktig type. Som et eksempel
Comment on lines +1081 to 1082
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the sentence grammar here.

The typo is fixed, but the sentence is still ungrammatical: blir sendt inn har en annen type. Please add og or rewrite it, e.g. argumentene som blir sendt inn, har en spesifikk type.

As per coding guidelines, **/*.nb.md: Check for Norwegian Bokmål (nb-NO) grammar and spelling with extra care.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@content/altinn-studio/v8/reference/logic/expressions/_index.nb.md` around
lines 1081 - 1082, The sentence "Funksjoner i uttrykkene har en forventning om
at argumentene som blir sendt inn har en spesifikk type." is
ungrammatical—update it to include the missing conjunction or comma for clarity
(e.g., "Funksjoner i uttrykkene har en forventning om at argumentene som blir
sendt inn, har en spesifikk type." or "Funksjoner i uttrykkene har en
forventning om at argumentene som blir sendt inn og har en spesifikk type.") so
the clause reads correctly; locate the exact sentence in the expressions
documentation (the paragraph starting with "Funksjoner i uttrykkene...") and
replace it with the corrected phrasing following Norwegian Bokmål grammar rules.

forventer funksjonen `equals` to strenger, men om du sender inn den boolske verdien `true` som det ene eller andre
argumentet fungerer det også fint, siden den boolske verdien `true` blir konvertert til strengen `"true"`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ example.validation.json
]
]
}
],
]
},
"definitions": {}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ example2.validation.json
"message": "en annen regel",
"condition": [...]
}
],
]
},
"definitions": {
"tegn-ikke-tillatt": {
Expand Down
Loading