Calculations
Last updated
Last updated
A Calculation, like a database Column, belongs to a Table. However its value, rather than being stored, is dynamically computed each time it is requested and whenever the inputs change, so it is always up to date.
Just like real database columns, calculations may be placed as fields on forms, used as data providers for different components, and requested programmatically.
To work with Calculations you need to open the Table where you will add them using the Table Editor.
To add a calculation to your solution, follow these simple steps:
In the Table Editor, select the Calculations tab at the bottom after all the Columns
Select the solution where you want to add the Calculation and click the Add
button
Set the property with a valid name.
Select the Return from the list
Select Open Selected Calculation
to edit the calculation in the javascript editor.
To edit an existing calculation, you will have to open it in the script editor and modify the code.
In the Table Editor, select the Calculations
tab
Select the Calculation you need to edit (expand the Solution or Module where the Calculation was defined if not visible)
You can enter a new name or change the Return Type inline, however, to change the supporting function you need to do the following:
Use the button "Open selected calculation" or double-click on the Calculation column to open the JavaScript file where the corresponding function is defined
Make the necessary changes to the function and make sure it returns a value of the same type as the Calculation definition
Save the editor.
In this example, we create a calculation called subtotal
on the order_details
table. The function will multiply a couple other columns in the record, quantity
and unitprice
. The result is returned from the function.
Each table can have a corresponding calculations file. For example, the orders
table can have an orders_calculations.js
file, wherein each calculation has a function by the same name. The function should return a value, which will be displayed wherever the calculation is referenced.
Similar to editing a Calculation, open the Table Editor, select the one you need to delete, and click on the button "Remove selected" at the bottom of the list (you will be prompted to confirm)
When deleting a calculation the supporting JavaScript function will also be deleted.
Unresolved Data Bindings When you edit the name of a calculation or delete it, you may also create errors in your application if you have objects bound to it, such as a field on a form. These errors can be resolved in Servoy Developer.
The scope of the JavaScript function is an individual record object. Therefore any of the record's other data providers (including other calculations), and Relations are immediately available, as well as global variables and globally related Relations.
Performance
Calculations may be called often. Therefore, developers should use discretion when implementing their JavaScript function. Most in-memory operations are very fast. However, actions that result in SQL queries or file operations may be slower and less predictable. For example, a calculation may be shown in a Grid, in which case it may be called hundreds of times.
Pro Tip
Calculations can be created with no return value so they can be used as extra columns at record level to store temporary data that belongs to a particular record.
We highly recommend trying our Get Started Guide, which has both a simple example of a calculation and an advanced example.
Please refer to the reference guide section on Calculations for a complete list of properties.