This article is also available in:
Calculated fields can be used to calculate values dynamically using other form fields as reference.

Example Imagine that you want to create a form to register purchase orders. It's likely that you include a table type field showing the articles that you want to buy, the quantity, unitary price and a "Total price" column that multiplies unitary price by quantity. In this case, it can be useful to include a calculated field below to save the total sum of the purchase.

Calculated fields allow you to save time and avoid mistakes. When completing a task, users won't be able to modify the value of a calculated field since it will be generated automatically.

Setting up a calculated field

Add a new field to the form, select "calculated" and then go to "Click to edit Calculated Field..."



Available functions:

Sum
Max
Min
Count
Mult
Script

After you select the function, you must choose the fields involved. These are the form fields that will be used to calculate the final value. Keep in mind that in order to set a calculated field correctly, first you'll need to add all involved fields to the form. Choose the $ mask if you want to show the symbol in the calculated field.



The Sum function allows you to define fields from the following types: text (simple or text area), integer number, decimal, combo box, table columns and/or other calculated fields.

Example: Given the table columns: Item, Unitary Price, Quantity and Total Price, we cam calculate first the values under Total Price. In order to do this, you must multiply "Unitary Price" by "Quantity" as shown in the following image:



Once you have done this, you can sum the values under this column in a calculated field called "Total purchase".



Warning: If the user enters non-numerical valyes in some of the fields involved, the final value for the function you chose will be 0. If the result of the sum is invalid, the value shown will be 0.

Calculated field visibility

Calculated fields can have 4 types of visibility:

Hidden: It isn't shown.
Inactive: The value of the calculated field won't update, no matter if the involved fields change or not. It allows you to 'freeze' a calculated field.
Active: The value of the calculated field is updated in real time whenever some of the fields involved change.
Required: The field is active.



Keep in mind that calculated field operations are carried out from visible form fields. If the function uses a hidden field, the sum will be 0, even if the field was assigned some value at an earlier stage.

Alike other form fields, you can establish conditions in the flow based on values from calculated fields and also insert these values in Send Tasks.

Scripts in auto calculated type fields

When placing scripts in auto calculated fields, it is necessary to keep in mind the following:

All references to the fields involved in a formula are made through Mustache format, between {{ y }}.
Example: {{Date of birth}}

We recommend that the script contains the logic that you want to execute, without much filler.
Example: {{ Area }} * {{ Price }}

If the logic is complex, a workaround can be to define it inside a function.
Example:

myFunction();
function myFunction() {
... logic ...
}


In the development of scripts such as these, all the recommendations mentioned in the post Script development in Flokzu apply, such as to use parseInt or parseFloat when math operations are made or the use of libraries for handling dates. Likewise, Flokzu's own functions (e.g. getFieldValue() ) can be used inside the script.

It's not possible to use the functions onChange and onInit in this type of script. They'll have no effect and their use can result in unwanted behaviors.
Was this article helpful?
Thank you!