Using Script Modules¶
A script module holds functions that several scripts share: workflow scripts of different tasks, forms, and external systems. This page explains how to write a module, how each caller runs it, and what to keep in mind for each caller.
Writing a Module¶
Every function assigned to exports is public. Other variables and functions in the module are private to it:
1 2 3 4 5 6 7 8 9 10 | |
Callers use the module name: Pricing.GetTotal(100).
Write each public function as exports.Name = function (...) { ... }. Public functions are found by this pattern, so other forms, such as assigning an object to module.exports, are not supported.
The module code runs again every time a script that can use it starts. Do not keep state in module variables between calls, and do not run slow operations, such as queries or service calls, at the top level of the module; put them in functions.
Calling from Workflow Scripts¶
Prework, postwork and other server scripts call module functions directly. The function runs in the caller's context, so it can use the same objects, such as $Xml and $WorkItem:
1 2 | |
Even so, pass the values a function needs as arguments instead of reading them from $Xml inside the function. The function can then be called from forms and external systems too, where these objects are not available. See Module Functions and RPC.
Calling from Forms¶
Form scripts run in the browser, so they call module functions on the server. For every public function, an asynchronous variant with the Async suffix returns a Promise:
1 2 3 | |
When the function throws an error, the Promise is rejected.
A call from a form runs on the server in a separate operation:
- It does not receive the form data,
$Xmlor$WorkItem. Pass the values the function needs as arguments, and apply the result in the form. - Arguments and results are sent as JSON. Use strings, numbers, booleans, plain objects and arrays. An Xml node returned by the function is received as XML text.
$ActiveUseris the user who called the function.- Changes made with
$Databaseare committed when the function completes and discarded when it fails.
The Async variants exist on the server too, so a function that is called the same way from forms and workflow scripts can use one code path.
Treat arguments from a form as untrusted input, and check the user before returning or changing data; see Security in Scripts.
Calling from External Systems¶
External systems call module functions through the REST API with the executeModule request, which runs the function of the process's active version. In addition to $ActiveUser, the function can read the request headers from $Headers and the cookies from $Cookies. See REST Service Calls.
Designing Module Functions¶
- Keep a function focused on one task, and give it a name that describes the result, such as
GetTotalorFindCustomer. - Return only the data the caller needs. Build a plain object from database rows instead of returning whole records:
1 2 3 4 5 6 7 8 9 10 11 12 | |
- Throw an error with a clear message when the arguments are not valid.
- Write diagnostic output with
console; see Debugging Scripts.