Skip to main content

Function

JavaScript is the main tool for data manipulation and transformation in Boltic Workflow. Use the Function activity to write custom JavaScript code that can transform data and execute complex logic. Additionally, you can integrate popular JavaScript libraries to further enhance the capabilities of your workflows.

Write and execute JavaScript code

You can create complex logic or manipulate data using JavaScript methods like map(). For example, you can use map() to transform an array of customer records:

const customers = API_1.result.customers;

return customers.map((customer) => ({
fullName: customer.name,
emailAddress: customer.email,
}));

Sample usage of axios

const req = {
url: "<your_url>",
data: {
a: 1,
},
headers: {
"x-data": 1,
},
};
const res = await axios.post(req.url, req.data, { headers: req.headers });

return res.data;

Sample usage of lodash

return _.defaults({ a: 1 }, { a: 3, b: 2 });

Sample usage of crypto

const hash = crypto.createHash("sha256").update("example").digest("hex");

return hash;

Sample usage of Buffer

const buffer = Buffer.from("Hello World", "utf-8");

return buffer.toString("base64");

Sample usage of querystring

const parsed = querystring.parse("name=boltic&status=active");

return parsed.name; // Output: "boltic"

Sample usage of url

const parsedUrl = url.parse("https://example.com/path?name=boltic");

return parsedUrl.query; // Output: "name=boltic"

Sample usage of FormData

const form = new FormData();
form.append("key", "value");

return form.getBuffer();

Accessing Data from Previous Activities

Data from previous activities can be accessed directly using the activity's name as a reference. This simplifies data retrieval by eliminating the need to access data using {{}} syntax.

Example:

const customerData = API_1.result.customers;
// Here API_1 is the name of any of the previous activity

return customerData.payload.map((customer) => customer.email);

Reserved System Keys

Certain keys are reserved by the system and cannot be used as variable names within your JavaScript code. Avoid using the following keys as variables:

  • payload - Represents the input data provided to the function.
  • global_variables - Stores global variables accessible across all activities.
  • result - Previously used for accessing the result of an activity; direct activity name references are now recommended.
  • secret - Reserved for handling sensitive credential data.
  • activity - Reserved for referencing activity-related metadata.
  • The names of previous activities - Use activity names directly to access their data.
  • resource - Reserved for resource-specific data.
  • operator - Represents the current operator executing the function.
  • execution_id - Identifies the unique ID for the current workflow execution.
  • workflow_id - Stores the unique ID of the workflow.
  • entity_id - Identifies the entity ID relevant to the current operation.
  • version - Represents the version of the workflow or activity.
  • execute_meta - Contains metadata about the execution context.
  • workflow_meta - Stores metadata about the workflow.
  • maximum_activity_completion_timeout - Defines the maximum time allowed for activity completion.
  • taskQueue - Refers to the task queue being used.
  • is_single_execution - Indicates whether the execution is a single instance.
  • chunk - Represents data chunking during execution.
  • workflow - Reserved for referencing the current workflow.

Using these names will cause conflicts and may result in errors or unexpected behavior.

Built-in libraries

To use a built-in JavaScript library:

LibraryDescription
axiosA promise-based HTTP client for making requests to APIs and handling responses.
lodashA utility library providing modular functions for manipulating arrays, objects, and strings.
cryptoA module for implementing cryptographic functionality like hashing and encryption.
BufferA global object in Node.js used for handling binary data.
querystringA module for parsing and formatting URL query strings.
urlA module for parsing, constructing, and resolving URLs.
FormDataA class to construct form-data streams for HTTP requests, supporting file uploads.

Advanced Options

For more information on advanced settings, see the Advanced Options documentation.