# JavaScript examples: return a status to branch on

> Return one status value from Run Custom Code and route on it with a Condition or Branches step. Includes priority, routing and approval examples.

Source: https://tadabase.com/docs/workflows/branching-status-snippets  
Status: Verified Nov 17, 2026. Checked against the current product

When the rule for choosing a path is too long for condition rows (keyword lists, allowed status changes, several criteria at once), work it out in a **Run Custom Code** step, return one status value, and let a Condition or Branches step route on it. For how the step works, see [Run custom JavaScript in a workflow](https://tadabase.com/docs/workflows/running-custom-javascript).

## Before you start

1. **Add the step** Add a **Run Custom Code** step to your workflow and leave **Language** set to **JavaScript**.
2. **Add the inputs** Under **Data to use in the code**, click **+ Add Input** once for each input the snippet lists. Type the name from the snippet in **Variable name** and choose where the value comes from, for example **Record Value (Source)** or **Custom Value**.
3. **Paste and test** Click the **Code** box to open the code editor, paste the snippet, and click **Run**. The **Response** tab shows what the code returned.
4. **Use the results** In a later step, open the value picker, choose **Step Data**, pick this step, and pick the key the snippet returned (for example `full_name`).

An input is available in the code as `$` followed by its variable name: an input named `first_name` is `$first_name`. Add every input a snippet uses; a name with no matching input stops the code with a "not defined" error.

> **Plain JavaScript only:** The code runs as plain JavaScript (ES2020 or later). There are no `Str`, `Num`, `Arr` or `DateTime` helper objects, so older examples that used them stop with an error. The step adds `returnData`, `fail`, `getLoggedInUser`, `getAppDetails`, `getAppVariable` and `getAppSecret`; see [Run custom JavaScript in a workflow](https://tadabase.com/docs/workflows/running-custom-javascript).

## Return a status, then branch on it

The code returns one value with a fixed key:

```js
// Input: lifetime_value
const ltv = Number($lifetime_value) || 0;

let tier = "bronze";
if (ltv >= 10000) tier = "platinum";
else if (ltv >= 2500) tier = "gold";
else if (ltv >= 500) tier = "silver";

returnData("tier", tier);
```

1. **Add the branching step** Below the code step, add a Condition step (one yes/no check) or a Branches step (one path per value; add paths with **+ Add Branch**).
2. **Pick the code step’s output** In the condition row, open the field picker, choose **Step Data**, pick the Run Custom Code step, and pick the key, here `tier`.
3. **Compare it** Choose an operator and type the value, for example **is** `gold`.

For a code step’s output the operators are **is**, **is not**, **contains**, **does not contain**, **starts with**, **ends with**, **is blank**, **is not blank**, **higher than**, **higher than or equal to**, **lower than** and **lower than or equal to**. The code step also offers **Status** and **Error**.

> **Write the key as plain text:** The field picker lists the keys it finds written as plain text in `returnData("key", ...)` calls. A key built from a variable, such as `returnData(name, value)`, still works at run time but does not appear in the list.

More about these steps: [Conditions and branches](https://tadabase.com/docs/workflows/conditions-and-branches).

## When you do not need code

The tier example above can be built without code: a Branches step with one branch per tier, each comparing the field with **higher than or equal to**. Use plain condition rows when the rule is a few comparisons on fields, for example a letter grade from a score, or "paid and shipped" from two Yes/No fields. Code earns its place for the cases below.

## Priority from keywords in the text

One pattern matches many words and phrases at once.

```js
// Input: body (the ticket text)
const text = String($body ?? "").toLowerCase();

let priority = "low";
if (/(outage|is down|cannot log ?in|losing money)/.test(text)) priority = "high";
else if (/(slow|error|broken|bug)/.test(text)) priority = "medium";

returnData("priority", priority);
```

## Check that a status change is allowed

Returns `allowed` or `blocked`, and a message you can show or log.

```js
// Inputs: from_status (the current status), to_status (the requested status)
const ALLOWED = {
  draft: ["submitted"],
  submitted: ["approved", "rejected"],
  approved: ["shipped"],
  rejected: ["draft"],
  shipped: []
};

const from = String($from_status ?? "").toLowerCase();
const to = String($to_status ?? "").toLowerCase();
const moves = ALLOWED[from] || [];
const ok = moves.includes(to);

returnData("move", ok ? "allowed" : "blocked");
returnData("error", ok ? "" : `Cannot move from ${from} to ${to}. Allowed: ${moves.join(", ") || "none"}`);
```

## Route by region

```js
// Inputs: country, state
const country = String($country ?? "").trim().toUpperCase();
const state = String($state ?? "").trim().toUpperCase();

let team = "central";
if (country !== "US") team = "international";
else if (["CA", "WA", "OR", "NV", "AZ"].includes(state)) team = "west";
else if (["NY", "NJ", "MA", "FL", "TX"].includes(state)) team = "east";

returnData("team", team);
```

## Stage from amount and days open

Pass today in with a **Current Date** input so the day count follows your app’s time zone.

```js
// Inputs: amount, opened_on ("2026-04-01"), today (Current Date)
const day = t => { const m = String(t ?? "").match(/^(\d{4})-(\d{2})-(\d{2})/); return m ? Date.UTC(+m[1], +m[2] - 1, +m[3]) : null; };
const amount = Number($amount) || 0;
const opened = day($opened_on);
const today = day($today);
const daysOpen = opened && today ? Math.round((today - opened) / 86400000) : 0;

let stage = "active";
if (amount >= 50000 && daysOpen < 30) stage = "hot_enterprise";
else if (amount >= 50000) stage = "stalled_enterprise";
else if (daysOpen > 60) stage = "stale";
else if (daysOpen < 7) stage = "fresh";

returnData("stage", stage);
```

## Approve automatically or send for review

Returns the decision and the reasons, so the review path can include them in an email.

```js
// Inputs: amount, order_age_days, reason
const reasons = [];
if ((Number($amount) || 0) > 100) reasons.push("amount over $100");
if ((Number($order_age_days) || 0) > 30) reasons.push("order over 30 days old");
if (!["damaged", "wrong_item", "not_received"].includes(String($reason ?? "").toLowerCase())) {
  reasons.push("reason needs review");
}

returnData("decision", reasons.length === 0 ? "approve" : "manual_review");
returnData("review_reasons", reasons.join("; "));
```

## Put a user in a test group, the same one every time

The share of users in the beta comes from an App Variable, so you can change it without editing the code. `getAppVariable` returns nothing for a variable that does not exist, and the code then uses 10.

```js
// Input: user_id   The same user always lands in the same group.
// Optional App Variable beta_sample_pct sets the share of users in the beta (default 10).
const id = String($user_id ?? "");
const sample = Number(getAppVariable("beta_sample_pct") ?? 10);

let hash = 0;
for (let i = 0; i < id.length; i++) hash = ((hash << 5) - hash + id.charCodeAt(i)) | 0;

returnData("variant", Math.abs(hash) % 100 < sample ? "beta" : "control");
```
