Reuse scripts
To use an existing script in a Scripted Decision node, create a library script containing the functionality you want to reuse and reference it from a next-generation journey decision node script.
A library script can take the format of any JavaScript code. You can also import functionality from another library script.
For example:
-
Create a library script using a minified third-party JavaScript utility library, such as
lodash.js
.Only import scripts from trusted third parties that you know take security seriously. It is your responsibility to ensure that third-party code is secure and to maintain it.
-
Write your own reusable snippet that enhances Identity Cloud debugging functionality.
Modules that use file systems, such as |
Create a library script
-
In the Identity Cloud admin UI, create a script of type
Library
. -
In the JavaScript editor, paste the contents of a minified third-party JavaScript library or write your own code.
Expose the reusable functions of your library script by defining properties on the
exports
object.For this example,
myExampleLibrary
defines and exports three functions:var i = 0; function add(j) {i += j}; function logTotal(log) { log.info("Total: " + i) }; // export constant exports.MSG = 'Final sum'; // export functions exports.add = add; exports.logTotal = logTotal; //direct export using an inline declaration exports.logTotalWithMessage = (log, message) => log.info(message + ": " + i);
A library script doesn’t have access to bindings, but you can pass in parameters. In the example, the
log
object is passed in from the scripted decision node that calls thelogTotal
andlogTotalWithMessage
functions.For similar functionality to library scripts, refer to the CommonJS modules.
You can’t create or export classes in library scripts, only functions and constants.
-
Save your changes.
Import a library script
-
In the Identity Cloud admin UI, create or edit a next-generation
Journey Decision Node
script.Only next-generation
Journey Decision Node
scripts support the use of library scripts.Alternatively, create or edit a
Library
script to nest library scripts. -
In the JavaScript editor, load the library using the
require(LIBRARY_SCRIPT)
notation; for example:var mylib = require('myExampleLibrary');
-
Access the exported functions using the library variable; in this case,
mylib
:mylib.add(1); mylib.logTotal(logger); mylib.add(3); mylib.logTotalWithMessage(logger, mylib.MSG);
-
Save your changes.