# Calculated (attribute of property)

calculated is an attribute of property.

## Description

The calculated attribute is used to automatically set the value of a property by calculating it using data from the same form. This calculation is done by an FPath expression.

The calculation trigger may be set using the calculatedtrigger attribute. This specifies the event that triggers the calculation of the value. Possible values are:

• onchange: the value is calculated when one of the properties on which the FPath expression depends is changed.
• onchangenew: the value is calculated only the first time that one of the properties on which the FPath expression depends is changed.

If the calculatedtrigger attribute is not set the property value is calculated onload and onchange.

## Usage

```<property
...
calculated = FPATH
calculatedtrigger= "onload" | "onchange" | "onchangenew"
/>
```

## Examples

• The subtotal is calculated by subtracting the discount percentage from the amount property. In turn, the total is calculated by adding the tax percentage to the subtotal value.
``` <entity label="Sale" name="Sale">
<property display="primary" label="Amount" name="amount" type="double"/>
<property label="Discount %" name="discount" type="double" />
<property label="Subtotal" name="subtotal" type="double" calculated="amount * (1 - (discount div 100))" />
<property label="Tax %" name="tax" type="double"/>
<property label="Total" name="total" type="double" calculated="subtotal * (1 + (tax div 100))" />
</entity>
```

The resulting form at work looks like this.

• The following example shows a more complex Invoice entity.

The subtotal of the invoice form is the sum of all the totalPrice values in the invoiceLines collection and the total of the invoice is calculated using the value for the discount property.

Each line in the collection calculates the value for the totalPrice property by multiplying the qty and unitaryPrice properties.

```    <entity label="Invoice" menu="Invoice" name="Invoice">
...
<property childproperty="invoice" collection="set" embedded="inline" entity="InvoiceLine"
extendable="extendable" label="Order details" labelposition="fieldset" name="invoiceLines"/>
<row>
<property name="subtotal" label="Subtotal" calculated="sum(invoiceLines[*].totalPrice)"
<property name="discount" label="Discount (%)" type="integer" default="0" />
<property name="total" label="Total" calculated="subtotal * (1 - (discount div 100))"
</row>
</entity>

<entity label="InvoiceLine" name="InvoiceLine">
<property entity="Invoice" label="Invoice" name="invoice" required="required"/>
<property label="Qty." name="qty" required="required" type="integer"/>
<property display="primary" label="Product" name="product" required="required" />
<property label="Unit Price" name="unitaryPrice" type="double"/>
<property calculated="qty * unitaryPrice" label="Total Price" name="totalPrice" type="double"/>
</entity>
```

The resulting form calculates properties based on the values entered for other properties.

## Important

• Calculated is not supported by collection properties.
• Calculated is not supported by 'selectdate' and 'selecttime' widgets.
• Calculated is not supported by boolean properties without specifying a 'widget'. In this case, you must use the select widget.
• Calculated is not supported by entity/enum type properties using the 'radio' widget.
• Calculated is not supported by entity/enum type properties using the 'combo' widget.
• Calculated is not supported by 'fixed' properties.
• Calculated is not supported by properties of types 'file' or 'image'.