Custom

Create custom logic using Node.js code blocks and built-in helper functions.

Node.js

You can write custom Node.js code blocks to apply your own logic to input data. Node.js includes a set of preinstalled helper functions to simplify the coding process.

The following components are available in the interface:
Console
Write Node.js code in the provided console window.
Supported helper functions
These helpers are designed to aid in specific tasks, making your code more concise and clear. Each helper function serves a limited, specific role to help the main logic of the code.
The available helper functions are as follows:
$require('lib name')
Node.js follows the CommonJS module system, and the built-in 'require' function efficiently includes modules from separate files. It reads a JavaScript file, runs it, and returns the exported objects. To include a library in your code, use the $require helper, which is a wrapper around Node.js’s native require() function. The following libraries can be used:
  • aws-sdk
  • q
  • request
  • rest
  • underscore
  • uuid
  • googleapis
  • redis
  • mongodb
  • lodash
  • events
  • crypto
  • path
  • querystring
  • url
  • xlsx
  • moment
  • soap
  • moment-timezone
  • htmlparser2
  • async
  • pubnub
  • pusher
  • htmlencode
  • buffer
  • http
  • https
  • punycode
  • string_decoder
  • assert
  • xml2js
  • ical-toolkit
  • postmark
  • zlib
  • ws
  • csvtojson
  • mime
  • node-uuid
  • hdb
  • netlify
  • deep-diff
  • cheerio
  • x12
  • openpgp
  • text-encoding
  • firebase-admin
  • syslog-client
  • upndown
Note: If you require any other libraries, contact IBM® Support.
$store
The $store key-value store is used for storing data during workflow execution. This store keeps data persistent for the current workflow.
$store.set(key, value, callback)

This method is used to set key-value pair. The ‘key’ must be of string data type, while the ‘value’ can be either JSON, string, or number.

Example - $store.set(“abc”, ”hello”, function(err){ if(!err){ $log(‘done’); } });

This stores the key "abc" with the value "hello".

$store.get(key, callback)

This method is used to retrieve the value of the key stored in $store.

Example - $store.set(“abc”, ”efg”, function(err){ if(!err){ // sets “abc” = ”efg” $store.get(“abc”, function(err, data){ // $log(data); }); } });

Account Store ($accountStore)

The key value 'accountStore' is used to store key-value pairs during workflow execution. It functions as a persistent data store, retaining the stored data.

The key difference between Account Store and Workflow Store is that Workflow Store ($store) stores data specific to a workflow. In contrast, Account Store ($accountStore) stores data tied to an account or user, allowing any workflow within the same account to access it.
$accountStore.set (key, value, callback)

This method is used to set key-value pair. The ‘key’ must be of string data type, while the ‘value’ can be either JSON, string, or number.

Example - $accountStore.set(“abc”,”hello”, function(err){ if(!err){ $log(‘done’); } });

This sets the "abc" key to the value "hello."

$$accountStore.get (key, callback)

This method is used to retrieve the value of the key stored in $accountStore.

Example - $accountStore.set(“abc”, ”efg”, function(err){ if(!err){ // sets “abc” = ”efg” $store.get(“abc”, function(err, data){ // $log(data); }); } });

Memory Store ($memoryStore)
You can store and retrieve one or more key-value pairs within a single workflow run. The keys that are stored in Memory Store are only active during the current execution, and cannot be accessed in subsequent runs of the same workflow.
$memoryStore.set(key, value, callback)

You can set a key-value pair. It is important to the 'key' must be of string data type, while the 'value' can be either string or object.

Example - $memoryStore.set("mem_store_1", "Hello", function(err, data){ $export(null, data); });

This sets the value for 'mem_store_1' key to 'Hello'.

$smemoryStore.get(key, callback)

This method is used to retrieve the value of the key stored in $memoryStore.

Example - $memoryStore.get("mem_store_1", function(err, data) { $export(null, data); });

This function gets the value of the 'mem_store_1' key.

$http.get(options)
This method is used to send an HTTP GET request and returns a promise. The options must be structured as follows:
var options = { url: url, qs: qs, headers: headers }
url

Fully qualified URI or a parsed url object from url.parse()

qs
Object that contains querystring values to be appended to the URI
headers
http headers (default: {})

Example - $http.get({url : “http://www.google.co.in”}) .then(function(data){ // data }).fail(function(err){ // http call failed see err });

$http.put(options)
It is used to send an HTTP PUT request and returns a promise. The options must follow this structure:
var options = { url: url, qs: qs, headers: headers, data: data }
url

Fully qualified URI or a parsed url object from url.parse()

qs
Object that contains querystring values to be appended to the uri
headers
http headers (default: {})
data
Entity body for PATCH, POST, and PUT requests. It must be a JSON-serializable object or String.

Example - $http.put({ url: “http://www.google.co.in”, data: {name: “Jenny”, location: “New York”} }).then(function(data){ // data }).fail(function(err){ // http call failed see err });

$http.del(options)
Use this helper function to send an HTTP PUT request. It returns a promise. The options must follow this structure -
var options = { url: url, qs: qs, headers: headers, data: data }
url

Fully qualified uri or a parsed url object from url.parse()

qs
Object that contains querystring values to be appended to the uri
headers
http headers (default: {})
data
Entity body for PATCH, POST, and PUT requests. It must be a JSON-serializable object or String.

Example - $http.del({ url : “http://www.google.co.in”, data : {name: “John”, location : “New York”} }).then(function(data){ // data }).fail(function(err){ // http call failed see err });

$export(err, output)
This helper function returns control from the code action. The "err" parameter indicates whether an error occurred, while the "output" parameter shows the returned output. Both "err" and "output" must be JSON-serializable objects or strings.

if err = null, the action is successfully completed.

if err!= null, the action encountered an error.

Example - For error $export(error);, for output data $export(null, data);

$success(output)
This helper function returns control from the code action, signaling that no error occurred. The output parameter provides the returned data. The output must be a JSON-serializable object or string.
$error(err)
It is used to return control from the code action and indicate that an error occurred during execution. err parameter returns an error. err must be a JSON-serializable object or String.
$log(data)

This helper function works similar to JavaScript's console.log(), and it prints or logs messages to track the flow of the program or debug issues.

Example - var str1 = "Hello"; $log(str1);

Timers
Example - var timeoutId = $setTimeout(function, milliseconds); var intervalId = $setInterval(function, milliseconds); $clearTimeout(timeoutId); $clearInterval(intervalId);
$setFlowStatus("failed", any error string or object)

This helper function sets the workflow execution status to failed. It is an optional parameter. You can pass an error message string or object to display when the workflow fails.

Example - this.$setFlowStatus("failed", "Some error occurred");