# Writing and testing functions

> Write a Python function that computes a result or reads and writes records, declare its data access, test it, dry-run it with Try it and read its runs.

Use a function when your app needs to work something out, or read and change records, in a way a plain field or workflow cannot. A function is a small piece of Python that takes an input and returns an output. Build one in the **Functions** screen, or ask Rad Bot to write it.

A function cannot reach secrets or files. It reaches an outside service only through a connection.

## What a function has

- A name, a label and a description.
- Code that defines `run(input)`, or `run(input, ctx)` if it uses records. It returns an object. The code can be at most 20,000 characters and runs for at most 5 seconds at a time.
- An input schema and an output schema. Each is an object with typed properties and a list of the required ones.
- Tests: an input and the output you expect.

Code can import only a fixed set of standard-library modules (maths, dates, time zones, text, JSON and the like). `from zoneinfo import ZoneInfo` works out a place's offset at any date. A few names are not available: `string.Formatter`, `typing.get_type_hints`, `typing.ForwardRef`, `dataclasses.make_dataclass`, `ast.iter_fields` and `ast.dump`. `operator.attrgetter` and `methodcaller` take public names only.

A function belongs to a module. Its tests must pass before the module can be checked in, and changing the function resets that. See [What a module is](/docs/modules/what-a-module-is).

## Read and write records

Write `run(input, ctx)` and use `ctx`:

| Need | Use |
|---|---|
| Read records | `ctx.records.get`, `get_many`, `list` |
| Create or change records | `ctx.records.create`, `ctx.records.update` |
| Write a log line | `ctx.log` |
| The time the run started | `ctx.now`, `ctx.today` |
| Who it runs as, and what started it | `ctx.user`, `ctx.trigger` |
| Call an outside service | `ctx.http` |

There is no delete. Use `ctx.now` rather than the clock: a run may be repeated internally after each read and must do the same thing every time. The **ctx reference** beside the code lists every call in one line each.

### Declare data access

In the function's **Data access** section, add one row per record type and say what the function may do with it: read, create or update. Declare the least it needs. Platform records (people, groups, roles, tasks and approvals) are never available. The access travels with its module and is covered by its tests.

### Who a function acts as

A run acts as the person who started it. It never does more than that person may, and never more than its declared access. A record the person cannot read looks the same as one that does not exist. Automations, workflow steps and webhooks run as the system, within the declared access.

### When changes are saved

Creates and updates are all checked first and saved after the function returns, so a run that fails changes nothing. A record a function creates starts that record type's workflow, like any new record. If something has to wait for a date, a person or an approval, put that in the workflow, not the function.

## Test a function

A test is an input and the expected output. To test reads and writes, a test can also carry:

- `records`: the sample records its reads see.
- `now`: a fixed start time.
- `expectedWrites`: the creates and updates it must make, in order. Write `"*"` for the id of a record it creates.

Tests never touch your real records. They check writes against the record types as they are now, so a change to a record type that would break the function makes its test fail. **Add a test with sample records** starts a test from the declared access. Results list each write with the values it sets, for example "Would set total = 12".

## Try a function on real records

On a saved function, **Try it** runs it once on your real records, as you. **Dry run (don't save changes)** is on by default: the writes are checked and shown, and nothing is saved. Switch it off to save them. The result shows the output, how many times the function read, the writes and the log.

## Read the run history

The function's **Runs** tab lists runs from the last 30 days, newest first. Each shows:

- what started it: Try it, a custom action, a workflow step, an automation or a webhook;
- how it ended: OK, a function error, a refused or partly saved write, a limit, or not run (not tested, skipped because it would loop, or skipped because of too many runs);
- how long it took, what it changed and what it logged.

A run made by an automation or workflow links to its record and says what kind of change caused it, never who. Inputs and outputs are not kept, because they may hold your end users' personal data.

See also [Running a function when records change](/docs/app-builder/automations-and-run-function-step) and [Custom actions: buttons that run a function](/docs/app-builder/custom-actions).

## What people see when a function fails

An end user sees the text of `raise ValueError("...")` if your function raises it, and a plain message for any other failure. You see the real error in the runs. Write the message so the person knows what to do next.

Two other messages can end a run:

- "Functions are paused on this instance." A RADP administrator has paused all functions on the workspace.
- "Functions are not available until this instance's next update." The environment has not yet been upgraded to support functions.

## Rad Bot and functions

Rad Bot can write functions. Its card names the records the function reads and writes, and adds tests with sample records for each read and write. Rad Bot offers the tests as a card next to the function's. It can also read a function's recent runs to find out why something went wrong.

For a function you already have, attach a text file holding its JSON (name, code, schemas, tests). The card takes the file exactly. See [Rad Bot in your workspace](/docs/rad-bot/using-rad-bot).

Source: https://radp.io/docs/app-builder/functions
Last updated: 2026-12-01
