Formula Columns
Compute the value of a column with a small JavaScript snippet that runs on the server every time an object is saved.
The Formula Columns plugin (formula-columns) lets you fill a column with a value that is computed from a small JavaScript snippet instead of being entered by hand. The snippet runs on the server during every save, so the calculated value is stored with the object like any other field value.
Typical uses are combining several fields into one display value, calculating a number from other fields, copying a value from a linked object, or generating an identifier. See Examples for ready-to-use snippets.
Enabling the plugin
formula-columns is not part of the fylr distribution and is not offered in the marketplace. Install it in the Plugin Manager as a url plugin from its release:
https://github.com/programmfabrik/fylr-plugin-formula-columns/releases/latest/download/fylr-plugin-formula-columns.zipOnce it is installed and enabled, a new option group "Formula Columns" appears in the column settings of the data model.
Configuring a formula
Open Data model → Object type → Columns, select the column that should hold the computed value, and open its Option tab. Enter the snippet under Formula Columns.
A column with a formula is still a normal column, so give it a type that fits the value you return (for example a text type for strings, a number type for numbers).
How the snippet runs
The plugin is a db_pre_save callback: it runs on /api/db before the object is written to the database, using the _all_fields mask so all object data is available. Your snippet is the body of an async function, so you can use await:
async function (objNew, objCurr, dataPath, dataPathCurr) {
// ... your snippet ...
}The return value of the snippet becomes the value of the formula column. The snippet is run with eval inside a try…catch (see Debugging below).
Available parameters
objNew
The new data of the object being saved — i.e. the fields of the object type (for a top-level object, its record; for a nested object, the nested record). This is where your own columns live, e.g. objNew.title.
objCurr
The data as currently stored in the database (the previous version). It is null on the first save, because there is no previous version yet.
dataPath
The path to the current data, starting at the top level. dataPath[0] is the object envelope — this is where the system fields such as _uuid, _id and _system_object_id live.
dataPathCurr
The same as dataPath, but for the current (stored) object data.
Utility functions and async
The formula runs inside an async function, so you can use await and standard browser/Node APIs such as fetch.
apiSearchBySIDs(sids, mode)
An async helper to look up objects by their system object id (_system_object_id). It accepts a single id or an array of ids and supports the formats long, short, long_inheritance, full and standard. It returns an array of the found objects (empty if none are found). See Read a value from a linked object.
Debugging and logging
Two tools help while you develop a formula:
Debug option — enable Debug on the column to store an event of type
FORMULA_COLUMNS_DEBUGfor each calculation, containing the input data and the result.Errors — if the snippet throws, the error is caught and stored as an event of type
FORMULA_COLUMNS_ERROR. The column is not changed in that case.
Inside the snippet you also have:
log— push messages that are stored in the system event, e.g.log.push({ info: info });info— instance information such as the APIurl, useful for API calls.
Examples
See Examples for complete, copy-ready snippets:
Last updated