Skip to content

Search Filter Behaviors

A search filter behavior is a behavior attached to one filter of a saved search. It runs in the search query panel, the form a user fills in before the search runs. A Custom Behavior on a filter is a short JavaScript script that can:

  • set a filter's value;
  • show or hide filters;
  • mark a filter invalid with a message.

It is best for pre-filling and checking what the user searches for, for example "open my own items by default".

At a glance

  • Runs In the user's browser, in the query panel of a saved search
  • Language JavaScript (QuickJS), 1.5 seconds per run
  • Authored in Search app > Builder > saved search > filter > Customize Filter > Behaviors > Custom Behavior
  • Can reach The filters of that saved search and the user name. No groups, no network, no results.
  • Best for Default values and simple checks on filters. Use a Custom Index Behavior for document properties.

When it runs

graph TD
  A["User opens the saved search"] --> B["Every filter behavior runs, in filter order"]
  B --> C["Query panel shows the filters"]
  C --> D["User changes a filter"]
  D --> E["Behaviors on that filter run"]
  E --> C
  C --> F["User runs the search"]
  F --> G{"Any filter invalid?"}
  G -->|Yes| H["Search does not run, the invalid filter gets focus"]
  G -->|No| I["Query uses every filter that has a value, hidden or not"]

Behaviors are saved with the saved search. Each saved search has its own filters and its own behaviors.

  • Search script There is no custom search script. A behavior changes filter values and the form, not the query the platform builds.
  • Query rewriting There is none. A script cannot add clauses, change operators or join searches.
  • Result actions There are no scriptable result actions. Scripts do not run on the results grid, on export or on bulk edit buttons. (Bulk edit runs the content type's Custom Index Behaviors, not filter behaviors.)
  • Services A behavior cannot call a service. There is no network access.

Where you author it

  1. Open the Search app and go to the Builder. You need the Create/Share Saved Searches permission.
  2. Open the saved search and select a filter.
  3. Select Customize Filter. On the Behaviors tab, find Custom Behavior and select Not Configured (or the behavior's name, to edit it).
  4. Enter a Custom Behavior Name and write the script. Select Test to run it in the preview panel.
  5. Select Save & Close, then save the search.

Runtime contract

Item Contract
Engine QuickJS (WebAssembly) in the user's browser
Runs when The query panel loads (every behavior, in filter order). The attached filter changes.
Timeout 1.5 seconds per run
Network access None. No fetch, XMLHttpRequest, require, import or timers.
Objects in scope panel, metadataKvp, builder, helper, context. No console.
context username only. usergroups and app are undefined.
Behavior types Every type the index panel offers for the filter's control, except Multi Key Automatic Indexing, AI Confidence and OCR Confidence
Identity filters A filter on a user or group allows only the default value behavior

Full details are in the Reference.

Rules

  1. Refer to filters by their display name, exactly as the Builder shows it, as one quoted string: panel["Assigned To"], builder.SetFilterValue("Assigned To", ...).
  2. Use the SetFilter... methods: SetFilterValue, SetFilterIsVisible, SetFilterIsValid, SetFilterValidationMessage.
  3. Read with panel["Filter Name"].Value. Every value is a string. An empty filter is "".
  4. Writes apply after the script ends. For each method and filter, the last call wins.
  5. Validate in this order: SetFilterIsValid(name, ok) first, then SetFilterValidationMessage(name, text) only when not ok.
  6. Keep scripts short. 1.5 seconds is the whole budget for one run.
  7. Do not use a top-level return. The script runs as one block with host code.

Gotchas

Watch out

  • No groups. context holds only username. context.usergroups.indexOf(...) throws. See Show or Hide Filters by Group.
  • Filter names are swapped for internal keys when you save. The editor replaces each quoted string that equals a filter name. A name built with +, or with different case, is not swapped and the script fails.
  • A value that equals a filter name is swapped too. If a filter is named Open, SetFilterValue("Status", "Open") writes a key. Rename the filter or build the value another way.
  • A hidden filter still filters. Its value still goes into the query. Clear the value when you hide a filter that must not apply.
  • An empty message shows an error text. SetFilterValidationMessage(name, "") shows "Could not find localized string."
  • Apostrophes in user names. The query panel puts the user name into the script in single quotes without escaping. A name with ' stops every behavior on the panel for that user. A \ changes the name.
  • Hiding is not security. The script runs in the user's browser. Restrict access with the search's sharing settings and repository permissions.

Examples

Copy this into your assistant

Paste the block below into your AI assistant together with the AI Assistant Primer, then describe what you want to build. It is the same contract as this page, written for a model.

Search filter behavior context for AI assistants
# Search filter behavior: context for AI assistants

You write KnowledgeLake search filter behaviors: short JavaScript scripts attached to one filter
of a saved search. They run in the search query panel, the form the user fills in before the
search runs.

## What can and cannot be done
- Can: set a filter's value, show or hide filters, mark a filter invalid with a message.
- Cannot: change the query the platform builds, add clauses, run code on results, export or
  bulk edit, or call any service. If the request needs one of these, say so and stop.
- Cannot: check the user's groups. `context` has `username` only. For group-specific filters,
  recommend one saved search per group, shared with that group.

## Runtime contract
- QuickJS (WebAssembly) in the user's browser. 1.5 seconds per run.
- Runs on query panel load (every behavior, in filter order) and when the attached filter changes.
- No network, no `console`, no `fetch`, no `require`, no `import`, no timers, no `Intl`.
- No top-level `return`: the script runs as one block with host code.
- On an error or timeout nothing applies and the filter shows
  "Behavior Custom Behavior could not be fired." The browser console (F12) shows the error.
- If any filter is invalid when the user runs the search, the search does not run and the user
  sees "Failed to execute search. Correct highlighted filters and try again."

## Objects
- `panel["Filter Name"]`: one entry per filter. `Value` (string, "" when empty), `IsVisible`,
  `IsValid`, `Style` (control kind).
- `metadataKvp`: the entry of the attached filter.
- `builder` (changes apply after the script ends; for each method and filter the last call wins):
  - `SetFilterValue(name, value)`: sets the value; does not run that filter's behaviors.
  - `SetFilterIsVisible(name, bool)`
  - `SetFilterIsValid(name, bool)`: false blocks the search; true clears the filter's messages.
  - `SetFilterValidationMessage(name, text)`
- `context.username`: the signed-in user's identity name. `context.usergroups` and
  `context.app` are undefined here.
- `helper.GenerateUUID()`

## Filter names (the part that bites)
- Write each filter's display name exactly as the Builder shows it, as ONE quoted string:
  "District Council". On save the editor swaps each quoted string that exactly equals a filter
  name for an internal key. Case-sensitive.
- A name built with + or assembled at run time is not swapped, and the script fails.
- Any quoted string equal to a filter name is swapped, even a value. Avoid value literals that
  equal a filter name.
- Two filters with the same name are "Name_1" and "Name_2", in filter order.
- `for (var k in panel)` gives internal keys, not names.

## Rules
1. Every value is a string. An empty filter is "" and is left out of the query.
2. A hidden filter still filters: its value stays in the query. Clear it when you hide it, if it
   must not apply.
3. Validation: `SetFilterIsValid(name, ok)` first, then `SetFilterValidationMessage(name, text)`
   only when not ok. NEVER pass "" as a message: it shows "Could not find localized string."
   Never put ";" in a message.
4. The behavior runs again whenever its filter changes. A script that fills an empty filter fills
   it again after the user clears it. Tell the user.
5. The query panel inserts the user name into the script in single quotes without escaping. A
   user name with an apostrophe breaks every behavior on the panel for that user.
6. Identity filters (users or groups) allow only the default value behavior. A script cannot be
   attached to them.
7. Hiding is not security.
8. Plain ES5 style: `var`, `function`. Never use `console`, `context.usergroups`, `context.app`.

## Example: default a filter to the current user
Attach to: Assigned To (a text filter). Reads Assigned To. Writes Assigned To.

```javascript
var current = (panel["Assigned To"].Value || "").toString();

if (current === "" && context.username) {
    builder.SetFilterValue("Assigned To", context.username);
}
```

## Output
- One script. Start it with a comment block: the filter it attaches to, the filters it reads and
  writes, what the user sees.
- Then tell the user how to attach it: Search app > Builder > open the saved search > select the
  filter > Customize Filter > Behaviors tab > Custom Behavior > Not Configured; name it, paste,
  select Test, Save & Close, then save the search. They need the Create/Share Saved Searches
  permission.
- If you do not know a filter's exact display name, ask. Do not guess names.