Global Scripts and React Scripts

Administration → Scripts manages three kinds of shared code that templates and prints simply select, so the code does not have to be repeated in each place separately. This article describes where and how these scripts are used in templates and prints. Creating, editing, importing and exporting them is described in the administrator article Scripts.

Script typeWhere it runsWhere it is selected
Scripts (Calculations)On the server, as part of the code of a task calculation section.The Scripts field on the task's Calculations tab.
Scripts (CO)In the user's browser – the form (dynamic conditions), the Case Overview or a standard print.The Loaded scripts field in the Conditions window, in the Case Overview builder and in the standard print detail.
Scripts (CO React)In the user's browser, only for the Case Overview and prints of type Print – React built on React components.The Loaded scripts field in the Case Overview / React print detail.

Scripts in task calculations

The Scripts field on the Calculations tab (see Calculations on a task and How and when calculations run) offers only active scripts of type Scripts (Calculations). The code of the selected scripts is inserted before the section code, so their functions are directly available in the section under their names – this is not an import but a literal concatenation of the code:

// Skript "CurrencyHelpers" v administraci obsahuje:
function toEur(amountCzk, rate) {
    return Math.round((amountCzk / rate) * 100) / 100;
}

// Sekce výpočtu, která tento skript vybrala v poli Skripty, ho může rovnou volat:
const rate = lib.getExchangeRate('EUR');
vars['TotalPriceEur'].setValue(toEur(vars['TotalPrice'].getValue(), rate));

If you select several scripts at once, they are inserted in the order in which they are selected in the field. If scripts have the same name or define the same variable/function, the one inserted later takes effect.

Scripts in dynamic conditions

The Conditions window (see Dynamic conditions: builder and Dynamic conditions: script) has a Loaded scripts field that selects which active scripts of type Scripts (CO) are loaded into the browser for the given task together with the condition code. If you select none, all active scripts of this type are loaded automatically.

Scripts in Case Overview and prints

Both the Case Overview and prints have the same Loaded scripts field as dynamic conditions; they just offer a different kind of script depending on whether it is the standard or the React variant:

  • The standard Case Overview and standard print offer Scripts (CO).
  • The Case Overview and prints of type Print – React additionally offer Scripts (CO React) – typically components such as custom tabs, tables or a progress bar.

If you switch the Print / Print – React toggle on a print, the Loaded scripts field is cleared, because each print type uses a different group of scripts and the existing selection would not make sense for the new group.

When to put a function in a script and when directly in the code
If the same logic (exchange rate conversion, company ID (IČO) validation, rendering the same React component) is used by several templates or several prints, it belongs in a shared script – a fix in one place then takes effect everywhere. Keep logic specific to a single task or a single print directly in its code, so you do not have to trace connections between the administration and the template.
Deleting or deactivating a script
If you delete or deactivate a shared script in the administration, it also disappears from all calculations, conditions, overviews and prints that used it. If their code refers to its functions, you will get an error on the next run/save. Before deleting a script, check everywhere it is selected.

Related: Scripts, How and when calculations run, Dynamic conditions: builder, Print templates.

Syca (opens in a new tab)

Powered by HelpDocs (opens in a new tab)