WorkflowsFunctionsSystem Functions
Run JavaScript Code
Write and run custom JavaScript code to perform advanced operations
Run JavaScript Code
Write custom JavaScript code to perform advanced operations that aren't available in other blocks. You can access your database, perform complex calculations, and manipulate data using programming code.
Technical Name: JavaScriptFunc
Properties
- Execution Mode: SYNC
- Type: NATIVE
- Category: System Functions
- Function ID:
f5b8d3e2-8e94-4c6b-9d7f-e3f4a2b8c901
Input Schema
Required Parameters
| Parameter | Type | Description |
|---|---|---|
jsCode | string | JavaScript code to execute |
Optional Parameters
| Parameter | Type | Description |
|---|---|---|
variableVsValueMap | object | Map of variable names to values |
outputVariables | array | List of variable names to include in output |
[Image placeholder: JavaScript function configuration]
Input Example
{
"jsCode": "const total = items.reduce((sum, item) => sum + item.price * item.quantity, 0);\nconst tax = total * 0.08;\nconst grandTotal = total + tax;\nreturn { total, tax, grandTotal };",
"variableVsValueMap": {
"items": [
{"name": "Product A", "price": 10, "quantity": 2},
{"name": "Product B", "price": 25, "quantity": 1}
]
}
}Use Cases
Data Transformation
Transform data between workflow steps:
const customer = {
firstName: input.first_name,
lastName: input.last_name,
fullName: `${input.first_name} ${input.last_name}`,
email: input.email.toLowerCase(),
registeredAt: new Date().toISOString()
};
return customer;Complex Calculations
Perform complex business calculations:
// Calculate tiered discount
let discount = 0;
if (orderTotal > 1000) {
discount = 0.15; // 15% for orders over $1000
} else if (orderTotal > 500) {
discount = 0.10; // 10% for orders over $500
} else if (orderTotal > 100) {
discount = 0.05; // 5% for orders over $100
}
const discountAmount = orderTotal * discount;
const finalTotal = orderTotal - discountAmount;
return {
orderTotal,
discount,
discountAmount,
finalTotal
};Data Validation
Validate and clean data:
const errors = [];
// Validate email
if (!email || !email.includes('@')) {
errors.push('Invalid email address');
}
// Validate phone
const phoneRegex = /^\+?[\d\s-()]+$/;
if (!phoneRegex.test(phone)) {
errors.push('Invalid phone number');
}
return {
isValid: errors.length === 0,
errors
};[Image placeholder: JavaScript transformation examples]
Database Access
Use the db object to query the database:
// Query data
const customers = await db.query(
'SELECT * FROM customers WHERE status = $1 LIMIT 10',
['active']
);
return {
customerCount: customers.length
};[Image placeholder: Database access examples]
Best Practices
- Keep it Simple: Use JavaScript for complex logic only
- Error Handling: Always wrap code in try-catch blocks
- Logging: Use console.log for debugging
- Performance: Avoid long-running operations
- Security: Never execute user-provided code directly
Error Handling Pattern
try {
// Your code here
const result = processData(input);
return { success: true, data: result };
} catch (error) {
console.error('Error:', error.message);
return { success: false, error: error.message };
}Related Functions
- Expression - Evaluate MAPL expressions
- SubFlow - Complex logic in separate flows
Limitations
- Execution timeout: 30 seconds maximum
- Memory limit: 256MB per execution
- Cannot import external npm packages
- Cannot make HTTP requests directly (use CallExternalApi)
[Image placeholder: JavaScript limitations]