Docs
On this page
concept3 min readApplication: Platform

Querying tables

How to read records in server scripts: single lookups, simple and complex queries, iteration, empty results, batches and safe traversal.

Querying tables means reading records in a server script with the Table object. This page explains which form of query to use, how to process the results, and how to keep large or uncertain queries safe.

One record or a query

Table has two forms. With a second argument it fetches one record and returns a TableRecord at once, so you can chain field calls. Without one it returns a Table, and you add operators and then read the results.

// One record: by ID, by lookup value, or by a criterion
let user = Table("User", "<record id>");
let sameUser = Table("User", EQUAL("UserName", "<user name>"));
let request = Table("#/ITSMRequest/<number>");

// A query: returns a Table
let activeUsers = Table("User").EQUAL("Active", true);

A single-record call returns null when nothing matches. When a criterion matches more than one record it throws PLATFORM-10003 (“Multiple records were returned when a single record was expected”). Test for null, and catch the error where duplicates are possible.

Simple and complex queries

A simple query chains operators on the Table. Chained operators combine with AND.

Table("User")
    .STARTS_WITH("UserName", "a")
    .BETWEEN("BirthDate", DateTime().withDate(1960, 1, 1), DateTime());

A complex query nests operators inside AND, OR and NOT. Indent each level so the logic stays readable.

Table("User")
    .OR(
        EMPTY("LastName"),
        AND(EQUAL("Active", true), NOT(IN("UserName", ["<user name>", "<user name>"])))
    );

Every operator and its arguments are listed in Query operators and criteria.

Process each record

The result methods run the query for you. Choose the one that fits what you do with the records.

You want to Use
Act on each record each(callback) or forEach(callback). The function receives the record and its index.
Build an array from the records map(callback). Return null, not undefined, to keep a blank entry.
Keep some records filter(callback).
Read records one at a time hasNext() and next() in a loop.
Process a very large result forEachPaginated(callback), which reads the database a page at a time.
// Callback form
Table("User").EQUAL("Active", false).each((user, index) => {
    log.info("User {} is {}", index, user.UserName());
});

// Loop form
let users = Table("User").EQUAL("Active", false);
while (users.hasNext()) {
    let user = users.next();
    log.info(user.UserName());
}

After next(), the Table also answers field calls for the current record, so users.UserName() reads the same value as user.UserName().

Handle a query with no results

Set a function with onNoResult. It runs, with the Table as its argument, whenever the query returns no records.

Table("User")
    .EQUAL("UserName", "<user name>")
    .onNoResult(table => log.warn("No user found"))
    .query();

When you expect exactly one record, executeSingle(callback) calls your function with it. With no record, it returns the result of the onNoResult function, or null. With more than one, it throws an error.

Work in batches

A query returns at most 10,000 records unless you set maxResults. The limit comes from the script.query.default_max_results application property. To read more, or to spread the work, process the records in batches. maxResults sets the batch size and firstResult the zero-based start. resultCount() returns the records in the current batch and totalResultCount() all matching records.

let batchSize = 100;
let start = 0;
let total = 0;

do {
    let users = Table("User").orderBy("UserName").maxResults(batchSize).firstResult(start);
    users.query();
    total = users.totalResultCount();

    log.info("Records {} to {} of {}", start + 1, start + users.resultCount(), total);
    users.each(user => processUser(user));

    start = start + batchSize;
} while (start < total);

Sort the query so each batch starts where the last one ended. forEachPaginated does the same work without the loop. It pages by record ID, so it neither skips nor repeats records that are deleted during the run. To count records without reading them, use Aggregate.

Follow references safely

Each reference you follow, such as user.Organization().Schedule(), is another database query. If a step is empty, the next call fails with a null pointer error and the script stops.

evaluate() on a TableRecord follows a dotted path and stops cleanly at an empty step. With one argument, it returns the value at the end of the path, or null. A path that ends in () returns the value. A path without brackets returns the Field object.

let user = Table("User", "<record id>");

// The schedule's name, or null if the user has no organisation or schedule
let scheduleName = user.evaluate("Organization.Schedule.Name()");

// The Name field of the schedule
let scheduleNameField = user.evaluate("Organization.Schedule.Name");

With two more arguments, evaluate() calls the first function with what it finds, or the second if a step is empty, and returns the function’s result.

let isScheduleActive = user.evaluate(
    "Organization.Schedule",
    schedule => schedule.Active(),
    () => false
);
Last updated 2 December 2026commit f6d7027b3395Chris Jones
Back to top