Skip to main content
Version: 4.60.0

Code

This component executes plain JavaScript code within a sandbox making it secure and safe.

note

Access to the console and the eval function are disabled.

Calling done​

Because JavaScript allows you to write code asynchronously, you need to call the done function when your code is done. When you are not using any asynchronous syntax, the done call should be at the end of your script.

// your code
done();
note

In cases where you are calling return in the global scope, you need to call done before the return statement.

note

The Code component does not utilize handlebars. To refer to incoming data, you can use value. followed by the data reference.

For example, if the incoming data is the following object:


{
"result": {
"_id": "123",
"kind": "RECORD"
}
}

To refer to the "kind" in the Code component, you can do something like:


const kind = value.result.kind;

Note that the Code component is the only place where you need to add value. before the incoming data reference.

Functions​

The following functions are available in the Code component:

send(value:any): void​

Sends data to the output of the component.

error(message:string, data:any): void​

Throws an error in the component. Like other components, the component will stop and the error will appear in the log panel unless the error output is connected.

setVariable(key:string, value:any): void​

Sets a global variable that can be used in any code component or HTTP component hook in the same flow.

getVariable(key:string): unknown​

Gets a global variable.

deleteVariable(key:string): void​

Deletes a global variable.

getFlowVariable(key:string, parseAs:string): unknown​

Gets a flow variable. By default parseAs is set to string. Options: 'boolean', 'array', 'number', 'object', 'date', 'string'. Note that the variable syntax %variableName% is not supported in the code component and that flow variables are read-only.

hexToBase64(value:string): string​

Converts a hexadecimal string to its Base64-encoded representation.

parseAs(value:string, parseAs:string): unknown​

Parses a value as something else. Options: 'boolean', 'array', 'number', 'object', 'date', 'string'. By default parseAs is set to string.

log(type:string, message:any, data:any): void​

Writes a log record. Type options: 'info', 'error', 'warn', 'debug'.

clone(value:object): object​

Clones an object.

md5(value:string): string​

Gets a md5 hash of the value.

sha1(value:string): string​

Gets a sha1 hash of the value.

sha256(value: string): string"​

Gets a sha256 hash of the value.

sha512(value:string): string​

Gets a sha512 hash of the value.

escapeRegexSpecialChars(value:string): string​

Escapes special characters in the given value string using a built-in regex pattern, ensuring safe use in regular expressions.

xpath(xml:string, expression:string): Node[] | string[] | number[] | boolean[] | null​

Extracts data from an XML string using the provided XPath expression. The function returns the extracted data as an array of nodes.

parseHtml(html:string, options?: CheerioOptions | null, isDocument?: boolean): Cheerio​

Parses an HTML string. The function returns a Cheerio object for the parsed HTML document, which mimics the behavior of jQuery. This method internally calls cheerio.load.

Example
const $ = parseHtml("<h1>Hello, world!</h1>");
log("info", $("h1").text()); // Output: Hello, world!
log("info", $.html()); // Output: <html><head></head><body><h1>Hello, world!</h1></body></html>

Check out the Cheerio docs for more information on how to select elements, traverse the DOM, and manipulate the DOM.

tip

To convert the entire parsed HTML document back into a string, use $.html(). This will return a full HTML document, including <html>, <body>, and <head> tags, even if these were not present in the original HTML document.

lookup(mappingName:string, lookupValues:array): Record<string, string>​

Gets mapping values previously loaded into the Content Store using the Load Mapping component. See the Load Mapping component documentation for more information.

getMappingKeys(mappingName:string): Promise<string[]>​

Gets the keys of a mapping.

lookupByKey(lookupKey:string): Record<string, string>​

Gets mapping values by their key. As opposed to the lookup function, this function requires the full lookup key.

lookupByField(mappingName:string, query:Record<string, string>): Record<string, string>​

Gets a mapping using only one key in case there are multiple. See the Load Mapping component documentation for more information.

Configuration​

Inputs | Outputs​


Code
Input
Output
Error
00 0 (ilxzu8s)
Any message sent with the send() function.