Helper
1. Function
Boltic Workflow supports the execution of custom JavaScript/Python code using the Function activity. This allows you to write complex logic and manipulate data within your workflows.
JavaScript
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,
}));
Built-in libraries
The following built-in libraries are available for use in your JavaScript code:
| Library | Description |
|---|---|
| axios | A promise-based HTTP client for making requests to APIs and handling responses. |
| Buffer | A global object in Node.js used for handling binary data. |
| crypto | A module for implementing cryptographic functionality like hashing and encryption. |
| FormData | A class to construct form-data streams for HTTP requests, supporting file uploads. |
| lodash | A utility library providing modular functions for manipulating arrays, objects, and strings. |
| papaparse | A powerful and fast CSV (Comma-Separated Values) parser for JavaScript, also supports streaming and web workers. |
| pdfParse | A simple and efficient Node.js library to extract text and metadata from PDF files. |
| querystring | A module for parsing and formatting URL query strings. |
| url | A module for parsing, constructing, and resolving URLs. |
| xlsx | A library for parsing and writing spreadsheet files (Excel), supporting formats like .xlsx. |
Examples
1. 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;
2. lodash
return _.defaults({ a: 1 }, { a: 3, b: 2 });
3. crypto
const hash = crypto.createHash("sha256").update("example").digest("hex");
return hash;
4. Buffer
const buffer = Buffer.from("Hello World", "utf-8");
return buffer.toString("base64");
5. querystring
const parsed = querystring.parse("name=boltic&status=active");
return parsed.name; // Output: "boltic"
6. url
const parsedUrl = url.parse("https://example.com/path?name=boltic");
return parsedUrl.query; // Output: "name=boltic"
7. FormData
const form = new FormData();
form.append("key", "value");
return form.getBuffer();
8. xlsx
const url = "<your_url>"
const response = await axios.get(url, {
responseType: 'arraybuffer',
});
const workbook = xlsx.read(response.data, { type: 'buffer' });
// Get the first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Convert the sheet to JSON
const data = xlsx.utils.sheet_to_json(worksheet);
return data;
9. papaparse
const readCSV = async (url) => {
try {
// Download entire file into memory
const response = await axios.get(url, { responseType: 'text' });
// Parse CSV from plain text
const parsed = papaparse.parse(response.data, {
header: true, // Treat first row as headers
skipEmptyLines: true,
});
console.log('Parsed CSV:', parsed.data);
return parsed.data;
} catch (error) {
console.error('Failed to fetch or parse CSV:', error);
throw error;
}
};
const csvUrl = '<your_url>';
const v = await readCSV(csvUrl);
return v;
10. PDF Parser
async function convertPdfUrlToMarkdown(pdfUrl) {
try {
const response = await axios.get(pdfUrl, { responseType: 'arraybuffer' });
const data = await pdfParse(response.data);
// Clean and format text for markdown
const markdown = data.text
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.map(line => `- ${line}`) // simple bullet formatting
.join('\n\n');
return markdown
} catch (error) {
console.error('❌ Error processing PDF:', error.message);
return error
}
}
const pdfUrl = '<your_pdf_url>'
return convertPdfUrlToMarkdown(pdfUrl, 'output.md');
Accessing Data from Previous Integrations
Data from previous integrations can be accessed directly using the integration'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);
Python
Python is a versatile and widely used programming language known for its simplicity and readability. Use the Function activity to write custom Python code that can transform data and execute complex logic. Additionally, you can integrate popular Python libraries to further enhance the capabilities of your workflows.
Write and Execute Python Code
You can create complex logic or manipulate data using Python methods, such as map(). For example, you can use map() to transform an array of customer records:
customers = API_1.result.customers
return [{'fullName': customer.name, 'emailAddress': customer.email} for customer in customers]
Built-in Libraries
The following built-in libraries are available for use in your Python code:
| Library | Description |
|---|---|
| base64 | A module for encoding and decoding data using base64 encoding. |
| bcrypt | A library for secure password hashing and verification. |
| Jinja2 | A library for templating, allowing you to generate dynamic content using placeholders. |
| json | A built-in library for working with JSON data. |
| numpy | A library for numerical computing, providing support for large, multi-dimensional arrays. |
| pandas | A powerful library for data manipulation and analysis, offering data structures and tools. |
| regex | A library for regular expression matching and manipulation. |
| requests | A popular library for making HTTP requests to APIs and handling responses. |
| ruamel.yaml | A library for working with YAML files, providing a flexible and extensible format. |
Example
1. Requests
import requests
url = "<your_url>"
data = {
"a": 1
}
headers = {
"x-data": 1
}
response = requests.post(url, json=data, headers=headers)
return response.json()
2. JSON
import json
data = {
"a": 1
}
return json.dumps(data)
3. base64
import base64
data = b"Hello World"
return base64.b64encode(data).decode('utf-8')
4. numpy
import numpy as np
data = np.array([1, 2, 3, 4, 5])
return data.tolist()
5. pandas
import pandas as pd
data = {
"a": [1, 2, 3],
"b": [4, 5, 6]
}
df = pd.DataFrame(data)
return df.to_dict(orient='records')
6. ruamel.yaml
import ruamel.yaml
data = {
"a": 1,
"b": 2
}
yaml = ruamel.yaml.YAML()
return yaml.dump(data)
7. jinja2
from jinja2 import Template
template = Template("Hello, {{ name }}!")
return template.render(name="John")
8. bcrypt
import bcrypt
password = "my_password"
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
return hashed_password.decode('utf-8')
9. regex
import regex
text = "Hello, World!"
pattern = r"Hello"
match = regex.search(pattern, text)
return match.group()
Accessing Data from Previous Integrations
Data from previous integrations can be accessed directly using the integration's name as a reference. This simplifies data retrieval by eliminating the need to access data using {{}} syntax.
Example:
customer_data = API_1.result.customers
# Here API_1 is the name of any of the previous activity
return [customer.email for customer in customer_data]
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:
| Key | Description |
|---|---|
| activity | Reserved for referencing activity-related metadata. |
| chunk | Represents data chunking during execution. |
| entity_id | Identifies the entity ID relevant to the current operation. |
| execute_meta | Contains metadata about the execution context. |
| execution_id | Identifies the unique ID for the current workflow execution. |
| global_variables | Stores global variables accessible across all activities. |
| is_single_execution | Indicates whether the execution is a single instance. |
| maximum_activity_completion_timeout | Defines the maximum time allowed for activity completion. |
| operator | Represents the current operator executing the function. |
| payload | Represents the input data provided to the function. |
| resource | Reserved for resource-specific data. |
| result | Previously used for accessing the result of an activity; direct activity name references are now recommended. |
| secret | Reserved for handling sensitive credential data. |
| taskQueue | Refers to the task queue being used. |
| version | Represents the version of the workflow or activity. |
| workflow | Reserved for referencing the current workflow. |
| workflow_id | Stores the unique ID of the workflow. |
| workflow_meta | Stores metadata about the workflow. |
Using these names will cause conflicts and may result in errors or unexpected behavior.