Function
Boltic Workflows 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,
}));
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();
Sample usage of 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;
Sample usage of 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;
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);
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. |
lodash | A utility library providing modular functions for manipulating arrays, objects, and strings. |
crypto | A module for implementing cryptographic functionality like hashing and encryption. |
Buffer | A global object in Node.js used for handling binary data. |
querystring | A module for parsing and formatting URL query strings. |
url | A module for parsing, constructing, and resolving URLs. |
FormData | A class to construct form-data streams for HTTP requests, supporting file uploads. |
xlsx | A library for parsing and writing spreadsheet files (Excel), supporting formats like .xlsx. |
papaparse | A powerful and fast CSV (Comma-Separated Values) parser for JavaScript, also supports streaming and web workers. |
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 like 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]
Sample usage of requests
import requests
url = "<your_url>"
data = {
"a": 1
}
headers = {
"x-data": 1
}
response = requests.post(url, json=data, headers=headers)
return response.json()
Sample usage of json
import json
data = {
"a": 1
}
return json.dumps(data)
Sample usage of base64
import base64
data = b"Hello World"
return base64.b64encode(data).decode('utf-8')
Sample usage of numpy
import numpy as np
data = np.array([1, 2, 3, 4, 5])
return data.tolist()
Sample usage of pandas
import pandas as pd
data = {
"a": [1, 2, 3],
"b": [4, 5, 6]
}
df = pd.DataFrame(data)
return df.to_dict(orient='records')
Sample usage of ruamel.yaml
import ruamel.yaml
data = {
"a": 1,
"b": 2
}
yaml = ruamel.yaml.YAML()
return yaml.dump(data)
Sample usage of Jinja2
from jinja2 import Template
template = Template("Hello, {{ name }}!")
return template.render(name="John")
Sample usage of bcrypt
import bcrypt
password = "my_password"
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
return hashed_password.decode('utf-8')
Sample usage of regex
import regex
text = "Hello, World!"
pattern = r"Hello"
match = regex.search(pattern, text)
return match.group()
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:
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]
Built-in libraries
The following built-in libraries are available for use in your Python code:
Library | Description |
---|---|
requests | A popular library for making HTTP requests to APIs and handling responses. |
json | A built-in library for working with JSON data. |
base64 | A module for encoding and decoding data using base64 encoding. |
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. |
ruamel.yaml | A library for working with YAML files, providing a flexible and extensible format. |
Jinja2 | A library for templating, allowing you to generate dynamic content using placeholders. |
bcrypt | A library for secure password hashing and verification. |
regex | A library for regular expression matching and manipulation. |
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.
Advanced Options
For more information on advanced settings, see the Advanced Options documentation.