# JavaScript examples: connected records

> How connected records reach a Run Custom Code step, and code that counts, totals, lists and checks values from them, with simpler no-code options.

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

Use these examples in a **Run Custom Code** workflow action when you need values from records connected to the source record: totals, averages, lists of names, or checks across children. New to the action? Start with [Running Custom Javascript](https://tadabase.com/docs/workflows/running-custom-javascript).

## Try a field or action first

Many roll-ups need no code:

- A [Rollup field](https://tadabase.com/docs/data/rollup) keeps a count, sum, average, minimum or maximum of connected records on the record itself, always up to date.
- The **Aggregate** workflow action counts, totals and summarizes a list of items or records inside a workflow. See [Count and total with Aggregate](https://tadabase.com/docs/workflows/count-and-total-with-aggregate).
- For a value per connected record, such as price times quantity, add an [Equation field](https://tadabase.com/docs/data/equation) on the connected table and roll it up on the parent.

Use code when you need something those cannot do, such as a list of distinct values, a text summary, or a check that combines several rules.

## How connected records reach your code

Under **Data to use in the code**, click **+ Add Input**, give it a **Variable name**, and pick its source. Four sources involve connections:

| Source | What your code gets |
| --- | --- |
| **Record Value (Source)**, with a Connection field picked | A list of the connected records' encoded IDs, such as `["DVWQWRNZ49", "4MXQJdrZ6v"]`. An empty list when nothing is connected. |
| **Connected Value** | One field from the connected records. One record gives a single value, several records give a list of values, none gives `null`. Choice fields (Radio, Select, Multi-Select, Checkbox) arrive as their text. |
| **Connected Record (Full Object)** | Every field of one connected record, as an object keyed by field slug (`field_56` and so on). Offered for connections where the source record points to one or more parent records; if there are several, you get the first. |
| **Action Record (Full Object)** | Every field of a record that an earlier step created, updated or found. |

**Connected Value** can also read child records: records in another table whose Connection field points to the source record. Child records are offered only in the **Run Custom Code** action. Up to 100 child records are read, the newest first; when there are more, the step adds a warning such as "Jobs: more than 100 records found. Used the newest 100."

> **One value or a list:** Because one record gives a single value and several give a list, start each example with a small `toList` helper (shown below) so the code works for zero, one or many records.

To see exactly what arrives, open the code window, go to the **Sample Data** tab, click **Choose Record**, and click **Run**. The inputs and their values show under **Parameters**.

## The toList helper

```js
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}
```

Paste it at the top of any example below that calls `toList`.

## Count and total a number field

```js
// Input: amounts (Connected Value: the Amount field of the child records)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const numbers = toList($amounts)
  .map(function (v) { return parseFloat(String(v).replace(/[^0-9.-]/g, "")); })
  .filter(function (n) { return !isNaN(n); });

const total = numbers.reduce(function (sum, n) { return sum + n; }, 0);

returnData("count", numbers.length);
returnData("total", Math.round(total * 100) / 100);
```

## Average of a rating field

```js
// Input: ratings (Connected Value: the Rating field of the child records)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const ratings = toList($ratings).map(Number).filter(function (n) { return !isNaN(n); });
const average = ratings.length
  ? ratings.reduce(function (a, b) { return a + b; }, 0) / ratings.length
  : null;

returnData("average_rating", average === null ? null : Math.round(average * 100) / 100);
returnData("rating_count", ratings.length);
```

## Comma-separated list of names

```js
// Input: names (Connected Value: a text field of the connected records)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const names = toList($names).map(String).filter(Boolean);
const unique = Array.from(new Set(names)).sort();

returnData("name_list", unique.length ? unique.join(", ") : "None");
```

## Count children by status

```js
// Input: statuses (Connected Value: the Status field of the child records)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const counts = {};
for (const s of toList($statuses)) {
  const key = String(s || "No status");
  counts[key] = (counts[key] || 0) + 1;
}

const total = toList($statuses).length;
returnData("open_count", counts["Open"] || 0);
returnData("done_count", counts["Done"] || 0);
returnData("all_done", total > 0 && (counts["Done"] || 0) === total);
```

Replace `Open` and `Done` with your option names exactly as they appear in the field.

## Percent complete

```js
// Input: statuses (Connected Value: the Status field of the tasks)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const statuses = toList($statuses);
const done = statuses.filter(function (s) { return s === "Done"; }).length;
const pct = statuses.length ? Math.round((done / statuses.length) * 1000) / 10 : 0;

returnData("pct_complete", pct);
returnData("project_status", pct === 100 ? "Complete" : pct > 0 ? "In progress" : "Not started");
```

## Latest date among connected records

```js
// Input: dates (Connected Value: a Date field of the child records)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const times = toList($dates)
  .map(function (d) { return Date.parse(d); })
  .filter(function (t) { return !isNaN(t); });

const latest = times.length ? new Date(Math.max.apply(null, times)) : null;
returnData("latest_date", latest ? latest.toISOString().slice(0, 10) : null);
```

`Date.parse` understands dates such as `2026-11-17` and `2026-11-17 09:30:00`. Run the code once with a sample record to check the format your field sends.

## Email list for all connected contacts

```js
// Input: emails (Connected Value: the Email field of the contacts)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const emails = Array.from(new Set(
  toList($emails).map(function (e) { return String(e).trim().toLowerCase(); }).filter(Boolean)
));

returnData("recipient_list", emails.join(", "));
returnData("recipient_count", emails.length);
```

## Find duplicate values among the children

```js
// Input: emails (Connected Value: the Email field of the contacts)
function toList(value) {
  if (Array.isArray(value)) return value;
  if (value === null || value === undefined || value === "") return [];
  return [value];
}

const seen = new Set();
const dupes = new Set();
for (const e of toList($emails)) {
  const key = String(e || "").trim().toLowerCase();
  if (!key) continue;
  if (seen.has(key)) dupes.add(key);
  seen.add(key);
}

returnData("duplicate_emails", Array.from(dupes).join(", "));
returnData("has_duplicates", dupes.size > 0);
```

## How many records a Connection field holds

```js
// Input: memberIds (Record Value (Source): a Connection field)
const ids = Array.isArray($memberIds) ? $memberIds : [];

returnData("member_count", ids.length);
returnData("has_members", ids.length > 0);
returnData("first_member_id", ids.length ? ids[0] : null);
```

The IDs come back in a form a later step can save into a Connection field.

## Read several fields of a parent record

```js
// Input: customer (Connected Record (Full Object): the Customer connection)
// Replace field_56 and field_57 with your field slugs.
const c = $customer || {};

returnData("customer_id", c.id || null);
returnData("customer_label", [c.field_56, c.field_57].filter(Boolean).join(" - "));
```

Field slugs (`field_56`) are shown when you type `$customer.` in the code window after loading sample data.
