Coding HubItems
A Coding HubItem contains reusable JavaScript for calculations and transformations that are clearer in code than in a table or visual rule. It executes in a secure platform sandbox with access to the modeled input, output, and supported execution context.
Use Coding for deterministic business logic. Use HTTP Client or connector nodes for network calls, and use a Workflow to orchestrate several steps.
Configure the contract
Select input and output Data Models in Settings. The Rich Editor uses those models to suggest paths and reduce spelling errors. A Coding HubItem should return only fields described by its output model.
Example input model:
{
"subtotal": 1250,
"discountPercent": 0.1,
"taxRate": 0.15,
"currency": "NZD"
}
Example output model:
{
"discountAmount": 125,
"taxableAmount": 1125,
"taxAmount": 168.75,
"total": 1293.75,
"currency": "NZD"
}
Worked example: calculate an order total
In the Rich Editor, calculate each intermediate value explicitly:
const subtotal = Number(input.subtotal || 0);
const discountPercent = Number(input.discountPercent || 0);
const taxRate = Number(input.taxRate || 0);
const discountAmount = subtotal * discountPercent;
const taxableAmount = subtotal - discountAmount;
const taxAmount = taxableAmount * taxRate;
output.discountAmount = Math.round(discountAmount * 100) / 100;
output.taxableAmount = Math.round(taxableAmount * 100) / 100;
output.taxAmount = Math.round(taxAmount * 100) / 100;
output.total = Math.round((taxableAmount + taxAmount) * 100) / 100;
output.currency = input.currency;
For financial calculations, agree on rounding rules with the business owner and test values that produce fractional cents. Do not assume the example rounding policy is correct for every jurisdiction.
Input, output, and context
inputcontains the request mapped into the Coding HubItem.outputis the modeled result returned to the caller or next workflow node.- The supported execution context can expose controlled value updates through suggestions in the Rich Editor.
Use local constants for intermediate calculations and write intentional final fields to output. This makes debugging easier than mutating input throughout the script.
Sandbox boundaries
Code is not running in a web browser or a general-purpose server process. Do not rely on:
- browser objects or page state;
- installed packages or module imports;
- local files or operating-system commands;
- unrestricted outbound network requests;
- shared memory surviving between executions.
These restrictions make execution predictable and keep tenant data isolated. Use modeled inputs, Global Variables for non-secret runtime configuration, connectors for credentials, and HTTP Client nodes for outbound calls.
Defensive code
Validate assumptions before calculation. For example:
if (input.subtotal == null || Number(input.subtotal) < 0) {
throw new Error("subtotal must be a non-negative number");
}
if (input.discountPercent < 0 || input.discountPercent > 1) {
throw new Error("discountPercent must be between 0 and 1");
}
Prefer a clear error over silently returning a plausible but incorrect result. Avoid including secrets or unnecessary personal information in error messages because errors can appear in execution history.
Work with collections
A Coding HubItem can transform modeled collections. For example:
const items = input.items || [];
output.activeSkus = items
.filter(item => item.active === true)
.map(item => item.sku);
output.activeCount = output.activeSkus.length;
If the same transformation can be understood in a Workflow with Filter and mapping nodes, prefer the visual form for business transparency. Use Coding when it materially improves clarity.
Test the code
Save cases for:
- a normal valid request;
- zero values;
- missing optional fields;
- invalid negative or out-of-range numbers;
- empty and large collections;
- rounding boundaries;
- every explicit error path.
Debug mode shows the HubItem's input, output, and supported context changes. Never use production customer data as an informal test fixture.
Use Coding in a workflow
Add a Coding node, select the reusable Coding HubItem and version, and map inputs and outputs. Some workflow configurations may also offer inline code. Prefer a reusable HubItem when the logic is shared, independently tested, or important enough to version separately.
Common problems
| Problem | Resolution |
|---|---|
| Suggested field is missing | Confirm the selected model and save its latest definition |
| Result field is absent | Assign it to output and include it in the output model |
Number becomes text or NaN | Validate and convert the input type before calculation |
| Script works alone but not in a workflow | Inspect the incoming edge mappings and deployed Coding version |
| Network or package API is unavailable | Move the call to an HTTP Client/connector or rewrite with supported language features |