Script Libraries
A Script library allows you to create reusable scripts to run across different server side capabilities of the platform including:
A Script library also helps to abstract complexities that would otherwise be exposed onto the respective platform capabilities mentioned above.
Creating a script library
Field name | Field type | Description | Sample value |
|---|---|---|---|
Name | String | Name of the script library | IncidentUtil |
Active | Boolean | Yes to activate the script library | Yes |
Description | Text | Use to provide high level information on what the Script library is for. | Utility functions for the Incident management process |
Script | Script | Classes and functions available within the Script library. The name of the Script library needs to be the same as the one defined in the script. E.g. IncidentUtil Just like any other server side scripting on the platform, Script library has access to Table API (Server) | (function (undefined) {
let IncidentUtil = function () {
if (!(this instanceof IncidentUtil)) return new IncidentUtil();
return this;
};
var log = Logger("IncidentUtil");
/*
* Function to find an Incident's related subtasks that are still opened
*
* @Params:
* _incRec - Table record of an Incident
*
* @Return:
* Array of incident task Table records
*/
IncidentUtil.getIncidentOpenTasks = function (_incRec) {
let retArr = [];
if (_incRec) {
_incRec.ParentIncidentForIncidentTasks.forEach(function(_taskRec) {
if (!_taskRec.Closed()) {
retArr.push(_taskRec);
}
});
}
return retArr;
}
// Can add more functions in the same script library
// Standard "require" vs "include" type construct
if (typeof module !== "undefined" && module.exports) {
module.exports = IncidentUtil;
}
return IncidentUtil;
})(); |
Calling a Script library
If we are to call a function (called IncidentUtil.getIncidentOpenTasks) on the sample Script Library above on a Trigger for the Incident table, it will look like the following:
// ... anything we want to do before calling the script library
let subTaskArr = IncidentUtil.getIncidentOpenTasks(current); // current is an Incident's Table record
// do what we want with subTaskArr