Script libraries
Fields of a script library, the template its script follows, and how other server scripts call its functions.
A script library holds server functions that other scripts reuse, so the logic lives in one place. This page lists its fields and shows the template, for administrators and developers with the administrator role.
Script libraries are under Administration → Business Logic → Script library. Triggers, workflow activities, inbound messages, record generators and Custom endpoints can all call them. See Where scripts run.
Script library fields
| Field | Type | Behaviour | Purpose |
|---|---|---|---|
| Name | Text | Required. Must match the name the script defines, for example IncidentUtil. |
Other scripts call the library by this name. |
| Active | Yes or no | Default yes. | Only active libraries can be called. |
| Description | Text | Required. | Says what the library is for, for example “Utility functions for the Incident management process”. |
| Script | Script | Required. Runs on the server and can use the Table. | The functions the library provides. |
Library template
This library defines one function, getIncidentOpenTasks, that returns an incident’s open tasks. The lines that matter are:
- the outer function, which keeps the library’s variables private
- the constructor named after the library,
IncidentUtil - the
module.exportsblock and the finalreturn IncidentUtil;, which make the library callable by name
(function (undefined) {
let IncidentUtil = function () {
if (!(this instanceof IncidentUtil)) return new IncidentUtil();
return this;
};
var log = Logger("IncidentUtil");
/*
* Returns the incident's tasks that are not closed.
* @param _incRec - Table record of an incident
* @return Array of incident task records
*/
IncidentUtil.getIncidentOpenTasks = function (_incRec) {
let retArr = [];
if (_incRec) {
_incRec.ParentIncidentForIncidentTasks.forEach(function (_taskRec) {
if (!_taskRec.Closed()) {
retArr.push(_taskRec);
}
});
}
return retArr;
};
// Add more functions here
if (typeof module !== "undefined" && module.exports) {
module.exports = IncidentUtil;
}
return IncidentUtil;
})();
Calling a library
A trigger on the Incident table calls the function by the library’s name:
let subTaskArr = IncidentUtil.getIncidentOpenTasks(current);
Inside a reclassification field map script, load the library first with require, for example let WorkflowUtil = require("WorkflowUtil");.