Portal Community

Example 1 — Customer Tier Calculation with Multi-Branch Logic

Determine a customer's discount tier based on their annual spend. The workflow variable annualSpend is injected as a JS global. The script returns both the tier name and the discount percentage.

// Workflow variables injected: annualSpend (number)

var tier;
var discountPct;

if (annualSpend >= 50000) {
  tier = 'platinum';
  discountPct = 20;
} else if (annualSpend >= 10000) {
  tier = 'gold';
  discountPct = 10;
} else if (annualSpend >= 2000) {
  tier = 'silver';
  discountPct = 5;
} else {
  tier = 'standard';
  discountPct = 0;
}

result = {
  tier: tier,
  discountPct: discountPct,
  discountAmount: Math.round(annualSpend * discountPct / 100 * 100) / 100
};
Outcome: For annualSpend = 15000: result = { tier: "gold", discountPct: 10, discountAmount: 1500 }. The downstream Switch node branches on output.CalculateTier.result.tier.

Example 2 — Date Arithmetic: Days Until Contract Expiry

Calculate the number of days remaining until a contract's expiry date stored as contractExpiry in workflow memory. Return an object with days remaining and an urgency flag.

// Workflow variables injected: contractExpiry (ISO date string, e.g. "2026-08-15")

var expiryDate = new Date(contractExpiry);
var today = new Date();
today.setHours(0, 0, 0, 0);

var msPerDay = 1000 * 60 * 60 * 24;
var daysRemaining = Math.ceil((expiryDate - today) / msPerDay);

result = {
  daysRemaining: daysRemaining,
  isExpired: daysRemaining < 0,
  isUrgent: daysRemaining >= 0 && daysRemaining <= 30,
  expiryDateFormatted: expiryDate.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })
};
Outcome: Returns { daysRemaining: 84, isExpired: false, isUrgent: false, expiryDateFormatted: "15 Aug 2026" }. The Switch node sends urgent renewal emails when isUrgent === true.

Example 3 — Zero-Pad Order Number Formatting

The system generates sequential integer order IDs. Format them as zero-padded 8-digit strings prefixed with the year for display in emails and PDFs.

// Workflow variables injected: orderId (number, e.g. 1234), orderYear (number, e.g. 2026)

var padded = String(orderId).padStart(8, '0');
var formatted = orderYear + '-' + padded;

result = {
  orderNumber: formatted,
  displayLabel: 'Order #' + formatted
};
Outcome: orderId = 1234, orderYear = 2026result = { orderNumber: "2026-00001234", displayLabel: "Order #2026-00001234" }.

Example 4 — Regex: Extract Version Number from a String

A software deployment webhook posts a message like "Deployed myapp v2.14.3 to production". Extract the semantic version number for downstream version-comparison logic.

// Workflow variables injected: deploymentMessage (string)

var versionPattern = /v(\d+)\.(\d+)\.(\d+)/;
var match = deploymentMessage.match(versionPattern);

if (match) {
  result = {
    found: true,
    version: match[0],        // "v2.14.3"
    major: parseInt(match[1]), // 2
    minor: parseInt(match[2]), // 14
    patch: parseInt(match[3])  // 3
  };
} else {
  result = { found: false, version: null, major: null, minor: null, patch: null };
}
Outcome: result = { found: true, version: "v2.14.3", major: 2, minor: 14, patch: 3 }. The Switch node can compare major/minor to determine if a breaking change was deployed.

Example 5 — Multi-Field Aggregation: Sales Summary

Given a salesRecords array in workflow memory, compute a summary object including total revenue, average order value, highest order, and count broken down by status.

// Workflow variables injected: salesRecords (array of { orderId, amount, status })

var total = 0;
var highest = 0;
var completed = 0;
var pending = 0;
var cancelled = 0;

for (var i = 0; i < salesRecords.length; i++) {
  var rec = salesRecords[i];
  total += rec.amount;
  if (rec.amount > highest) highest = rec.amount;
  if (rec.status === 'completed') completed++;
  else if (rec.status === 'pending') pending++;
  else if (rec.status === 'cancelled') cancelled++;
}

var avg = salesRecords.length > 0 ? Math.round(total / salesRecords.length * 100) / 100 : 0;

result = {
  totalRevenue: Math.round(total * 100) / 100,
  averageOrderValue: avg,
  highestOrder: highest,
  orderCount: salesRecords.length,
  byStatus: { completed: completed, pending: pending, cancelled: cancelled }
};
Outcome: A rich summary object ready for the management dashboard email or Slack notification node. All fields are computed in a single pass through the array.