A backend script is server-side JavaScript automation that lives in a group (or, for an instance admin, at the instance level). It runs unattended — on a schedule, when an item event fires, as part of saving an item, from an item’s context menu, or on demand — and can read and write the group’s items, query across the group, call external APIs, and read secrets.
Note
Backend scripts are JavaScript only. Gonja (Jinja2-style) templates power scripted widgets instead — they render read-only HTML and cannot automate anything.
For the full API — user, group, item, fetch, secrets, and the rest — see the JavaScript API reference. This page covers what a backend script is: how to create one, what each trigger does, who it runs as, and how writes behave.
Creating a script#
From a group’s Scripts admin page, open the script editor and choose what kind of script to create:
- Automation — Manual, Schedule or Event. Manual and scheduled runs work against the group in general, with no item in scope; an event script runs when an item is created or a field is changed, with that item in scope.
- Item script — Save Hook or Menu. Bound to one item type and runs against a specific item.
Write the JavaScript in the built-in editor (an AI assistant can draft or edit it for you), set the trigger’s configuration — a cron expression for a schedule; for an event, which actions run it, an optional item type and field conditions; an icon for a menu entry; field conditions for a save hook or menu visibility — and save. A syntax error is rejected at save time, before the script can run.
Note
Event scripts are reserved to group administrators. Besides the editor, the configuration assistant in group administration can create and edit them; the Timill assistant cannot, and points you to group administration instead.
Trigger types#
| Trigger | Runs when | Item in scope? |
|---|---|---|
| Manual | An admin clicks Run in the scripts admin | No |
| Schedule | Its cron expression fires | No |
| Event | An item is created, or one of its fields is changed — whichever the script listens for — and its conditions match | Yes — the created or changed item |
| Save hook | An item of the bound item type is saved (including on creation), after the save’s own changes are applied but before they are persisted | Yes — the item being saved |
| Menu | Someone picks the script from an item’s context menu | Yes — the clicked item |
Save hook and menu scripts are bound to one item type when created; an event script can be limited to one item type or listen to all of them. Event and save-hook scripts can also carry conditions (field-level rules built in the editor) that gate whether they fire at all; a menu script’s conditions instead gate whether the menu entry is shown.
Manual#
Runs only when an admin clicks Run in the scripts admin. Nothing is in scope automatically — read items with group.queryItems(query). Because nobody is watching the script run, log a summary of what it did; the execution history is the only record.
Only one run of a given script executes at a time — starting a second run while one is in flight is refused, never queued.
Schedule#
Runs unattended on a cron expression, with no item in scope. The expression is standard 5-field cron — minute, hour, day-of-month, month, day-of-week — with no seconds field; @hourly, @daily, @weekly, @monthly and @every 30m are also accepted. It fires in the server’s local time zone unless the expression itself carries one (CRON_TZ=Europe/Paris 0 7 * * *). 0 7 * * 1-5 means 07:00 on weekdays.
Runs never overlap: if the previous tick is still executing when the next one is due, the next tick is simply skipped rather than queued.
Because a schedule runs unattended and repeatedly, write it to be idempotent — see Idempotency below.
Event#
Runs when one of the script’s configured platform actions occurs (item created, updated, deleted, a field updated, status changed, assigned, or a comment added), gated by its conditions. It can fire many times in quick succession — once per item in a bulk edit, for example — so keep it cheap and idempotent.
When the action that fired the event was on an item, that item is available as item; an action raised by something else leaves it undefined, so guard before using it:
if (typeof item !== "undefined" && item) {
// use item
}
Writes made from an event script persist immediately (see the table below). Be careful that a script’s own writes cannot re-trigger the same event and loop.
Save hook#
Runs synchronously as part of saving an item of its bound type — including item creation — while the person is waiting on their save. item already carries the values just entered; read them directly (item.title, item.getField(id)) rather than re-querying for the item.
Because this trigger fires on every matching save, there is no “previous value” to compare against — you’re always looking at the state after the edit. “When the status changes to X” cannot be expressed inside the script; write the state test (“the status is X”) and use the editor’s conditions to control which saves run it at all.
If the script throws, the error is logged, any staged changes are dropped, and the person’s save still succeeds — they will not see an error, so log what went wrong.
Menu#
Runs on demand when someone picks the script from an item’s context menu, and they wait for it. item is the clicked item. The script’s log output (or, if it logged nothing, its return value — unless that value is an object or array) is shown to the user in a toast, capped at about 500 characters. Field changes reach any open page through the normal live updates; nothing needs to be refreshed from the script.
The menu entry can be clicked more than once, and nothing about it is transactional: if a script writes several fields and then throws, the earlier writes stay written. Order writes so a partial run leaves the item in a sensible state, and check whether the work was already done before repeating something that shouldn’t happen twice (see Idempotency).
Acting user#
Every run has an identity it executes as — this is whose permissions field writes and queryItems are checked against, and what a query’s $me resolves to.
| Trigger | Acting user |
|---|---|
| Manual | The admin who clicked Run |
| Schedule | The script’s author ($me in a query means the author, not “the user this item concerns”) |
| Event | The user who performed the triggering action, when known |
| Save hook | The user performing the save, when known |
| Menu | The user who clicked the menu entry |
For event and save-hook scripts, if no acting user can be resolved from the request (a background or system-initiated change), the script falls back to running as its own author.
Globals#
What’s available depends on the trigger, because it decides whether there’s an item to expose:
| Global | Manual / Schedule | Event | Save hook / Menu |
|---|---|---|---|
user, userId | yes | yes | yes |
group, groupId | yes | yes | yes |
item, itemId | no | only when the action was on an item — guard with typeof item !== "undefined" | yes |
fetch, secrets | yes | yes | yes |
pageVars | empty object (no page) | empty object | empty object |
api, echarts, log / console.log | yes | yes | yes |
pageVars exists on every script for API consistency with scripted charts, but backend scripts never run on a page, so it is always empty. See the JavaScript API reference for what each global offers, and Field values for what getField/setField accept per field type.
Writes: immediate or staged#
Outside a save hook, every write method (setField, setTitle, setStatus, createItem, addComment, …) persists as soon as it’s called — save() exists but does nothing extra.
Inside a save hook, field setters behave differently: they stage a change rather than writing it, and the platform persists every staged change together with the edit that triggered the hook, as part of the same save and the same history entry. item.save() is a no-op there. A small number of writes are never staged, even in a save hook — they always happen immediately.
| Write | Outside a save hook | Inside a save hook |
|---|---|---|
setField, setTitle, setDescription, setStatus, setAssignedTo, setDueDate | Persists immediately | Staged — persisted together with the triggering save |
addAttachment | Persists immediately | File is stored immediately; the field change joins the staged save |
removeAttachment | Persists immediately | File moves to the trash immediately; the field change joins the staged save |
addComment | Persists immediately | Immediate, even in a save hook |
item.delete() (the item being saved) | Moves it to the trash immediately | Applied once the triggering save completes |
group.deleteItem(id) / .delete() on any other item | Always immediate | Always immediate |
group.createItem(...) | Always immediate | Always immediate |
Every write method returns {success: true} or {success: false, error: "…"} and never throws — check success when it matters. Reads that cannot work — a malformed query, a file that isn’t on the item — throw instead, so wrap them if the failure is expected. Staged writes still go through the acting user’s field permissions: a field they may not edit is silently skipped (and logged), not written.
Deletion is always a soft delete: the item moves to the group’s trash bin, from which an admin can restore it.
Idempotency for event and scheduled scripts#
Event scripts can fire repeatedly in a short window (a bulk edit fires one event per item), and scheduled scripts run the same code over largely the same data on every tick. Anything with an outside effect — posting a comment, calling a webhook, sending an email — will repeat unless the script guards it with state the next run can see: a field the script itself sets, an existing comment, or a query that already excludes items it has handled.
Save hooks have a related but different pitfall: since a hook only ever sees the item after the edit, staging the same value it already holds is simply dropped — it costs nothing and adds no history entry — so a value-based hook doesn’t need an equality guard. What it can’t do is detect a transition; use the editor’s conditions for that instead.
Execution history and logging#
Every run of a backend script — whatever its trigger — is recorded in the script’s execution history (and so is every render of a scripted widget or chart — see Limits and errors): status (success, failed, timeout, or cancelled), start time and duration, resource usage, and everything passed to log() / console.log() during the run. A script’s return value is recorded separately as its output.
Find it from the scripts admin page: open a script and view its execution history. This is the primary way to debug a scheduled or event script, since nobody watches those run directly.
Limits#
Backend scripts run with a 30-second timeout (5 seconds for a save hook, since a person is waiting on the save), up to 100 API calls, and a 50MB memory ceiling. Only fetch() and secrets.get() count as API calls — queryItems and every other read or write do not. See Limits and errors for the full picture, including scripted widgets and charts.
Examples#
Auto-assign on creation (save hook, bound to a task item type):
if (item.getField("priority") === "urgent" && !item.getField("assignee")) {
item.setAssignedTo(userId); // stages; persists with this save
}
Notify on status change (event script, trigger: field update on status):
if (typeof item !== "undefined" && item) {
log(`${item.title} is now ${item.status}`);
const token = secrets.get("NOTIFICATION_WEBHOOK_TOKEN");
if (token) {
fetch("https://hooks.example.com/notify", {
method: "POST",
headers: { Authorization: "Bearer " + token },
body: { item: item.title, status: item.status },
});
}
}
Weekly cleanup (schedule, 0 7 * * 1):
const stale = group.queryItems("completedDate:<$today-30days");
log(`found ${stale.length} items completed over 30 days ago`);
for (const it of stale) {
const r = it.setField("archived", true); // immediate — no item in scope here
if (!r.success) log(`could not archive ${it.title}: ${r.error}`);
}
log(`archived ${stale.length} items`);
Create a follow-up task (menu script, on demand):
const already = group.queryItems(`title:"Follow-up: ${item.title}"`);
if (already.length > 0) {
log("Follow-up already exists — nothing to do");
} else {
const res = group.createItem("task", {
title: `Follow-up: ${item.title}`,
assignee: item.assignedTo,
});
log(res.success ? "Created follow-up task" : `Failed: ${res.error}`);
}
Validate a required field (save hook):
if (item.status === "in-review" && !item.getField("estimate")) {
log("Warning: item moved to review without an estimate");
// A save hook cannot block the save or show the user anything directly —
// use the editor's Mandatory Field rules for hard validation instead.
}
For more, see the cookbook.