Skip to content

Custom Index Behaviors

A Custom Behavior is a short JavaScript script attached to one field of a content type. It runs in every index panel that shows the content type, and on the server in some workflow steps. It can:

  • show or hide fields;
  • make them read-only;
  • set values;
  • replace a choice list;
  • mark a field invalid with your own message.

It is best for indexing rules the user must see while they work: "show the approval fields only when review is needed", "a PO number is required over 500".

At a glance

  • Runs In the browser in every index panel, and on the server in the Index Your Documents, Run Indexing Behaviors and Start QC Task steps and in Search bulk edit
  • Language JavaScript. QuickJS in the browser, V8 on the server. 5 seconds per run.
  • Authored in Connections app > content type > field > Behaviors > Custom Behavior
  • Can reach The fields of one content type. No network, no work item metadata, no file name.
  • Best for Interactive indexing rules. Use a Custom Step for unattended logic.

When it runs

A behavior reacts to its own field. The panel load runs every behavior. A value that one behavior sets can start the behaviors of another field. Four server paths run every behavior with no user and no screen.

graph TD
  A["Panel loads"] --> B["Every behavior runs, in field order"]
  C["User changes a field"] --> D["Behaviors on that field run"]
  D --> E["Script sets another field with SetFieldRuntimeValue"]
  E --> F["Behaviors on that field run, unless suppressed"]
  G["Run Indexing Behaviors step"] --> J["Every behavior runs on the server, in V8"]
  H["Start QC Task step, Mark Valid Documents as Indexed = Yes"] --> J
  I["Index Your Documents step or Search bulk edit"] --> J

Start QC Task runs behaviors

Its setting Mark Valid Documents as Indexed is Yes by default, so a script you wrote for the index panel also runs on every work item that reaches a QC task.

The server paths do different things with the result. See Server runs in the reference.

Where you author it

  1. Open the Connections app. You need permission to edit configuration.
  2. Open the content type and select the field that triggers the script.
  3. Open Behaviors, select Add a Behavior, and choose Custom Behavior.
  4. Enter a Custom Behavior Name and write the script in the editor. Select Test to run it in the preview panel.
  5. Select Save & Close. If the field has more than one behavior, set the run order with Change Behavior Order.

Use a built-in behavior first when one does the job

These are configuration, not code:

  • Required
  • Alternate Requirement Behavior
  • Input Mask
  • Default Text
  • Source Lookup
  • Filter
  • Automatic Indexing
  • Source Validation

The public API can also add a behavior:

  • API V1_RepositoryAPI
  • Action AddContentTypeFieldBehavior
  • Behavior key CustomJSBehavior
  • Parameters BehaviorName and CustomJSBehavior (the script text)

Runtime contract

Item Contract
Engine Browser: QuickJS (WebAssembly). Server: V8, a new engine for each run.
Runs in the browser Panel load (every behavior, in field order). The attached field changes. Another behavior sets the attached field without suppressing behaviors.
Runs on the server Index Your Documents, Run Indexing Behaviors, Start QC Task (when Mark Valid Documents as Indexed is Yes) and Search bulk edit
Timeout 5 seconds per run, browser and server
Network access None. No fetch, XMLHttpRequest, require, import or timers.
Objects in scope panel, metadataKvp, builder, helper, context. No console.
context in the browser username (the signed-in user), usergroups (group names), app (for example "webindex", "qc")
context on the server username is "", usergroups is [], app is "worker"

The full object list is in the Reference.

Rules

  1. Read with panel["FieldName"].RuntimeValue. Write with builder methods.
  2. Field names are exact and case-sensitive, spaces included.
  3. Every value is a string. A Boolean field reads "true" or "false".
  4. Writes apply after the script ends. A value you set is not readable in the same run.
  5. For each method and field, the last call wins.
  6. A behavior reacts to its own field. If the result depends on two fields, attach the same script to both.
  7. Pass true as the third argument of SetFieldRuntimeValue when you set another field and do not want its behaviors to run.
  8. Write the script so it gives the right result every time it runs: on panel load, on change, and on the server.
  9. Do not use a top-level return. The script is joined to host code and runs as one block. Use if/else, or put the code in a function and call it.
  10. Validate in this order: SetFieldIsValid(field, ok) first, then SetFieldValidationMessage(field, text) only when not ok.

Gotchas

Watch out

  • An empty message shows an error text. SetFieldValidationMessage(field, "") shows "Could not find localized string." Clear messages with SetFieldIsValid(field, true).
  • builder.SetChoices is broken. It references an undefined variable and the behavior fails. Use builder.SetFieldChoices(metadataKvp.Key, choices).
  • Context.Username does not exist. The object is context, all lowercase: context.username, context.usergroups, context.app.
  • There is no console. console.log throws and nothing applies. See Debugging Custom Index Behaviors for what to do instead.
  • The server has no user. On the server a script that writes context.username writes "", and a group check never matches. Skip user-dependent code when context.app === "worker". This matters for every tenant that uses Start QC Task.
  • Server validity has effects. A field that a script marks invalid on the server stops the document from being marked as indexed. In Start QC Task that sends the work item to a person.
  • Load runs overwrite. A script that stamps a value runs again whenever anyone opens the document. Write only when the target is empty.
  • Two scripts that set each other's fields loop until the 5 second limit. Suppress behaviors on one side.
  • Hiding is not security. A hidden field keeps its value, and the script runs in the user's browser. Enforce rules in the workflow.

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.

Custom Index Behavior context for AI assistants
# Custom Index Behavior: context for AI assistants

You write KnowledgeLake Custom Behaviors: short JavaScript scripts attached to one field of a
content type. They run in the index panel in the browser, and on the server in workflow steps.

## Runtime contract
- Browser: QuickJS (WebAssembly). Server: V8. 5 seconds per run in both.
- Runs on panel load (every behavior, in field order), when the attached field changes, and when
  another behavior sets the attached field without suppressing behaviors.
- Also runs on the server, with no user and no screen, in these places: the Index Your Documents
  step, the Run Indexing Behaviors step, the Start QC Task step when its setting "Mark Valid
  Documents as Indexed" is Yes (the default), and Search bulk edit.
- No network, no `console`, no `fetch`, no `XMLHttpRequest`, no `require`, no `import`, no
  timers, no `Intl`.
- The host joins the script to its own code and runs it as one block. A top-level `return` is a
  syntax error. Use if/else, or wrap the code in a function and call it.
- On an error or timeout nothing applies and the field shows
  "Behavior Custom Behavior could not be fired." The browser console (F12) shows the error.

## Objects
- `panel["FieldName"]`: one entry per content type field. Properties: `Key`, `RuntimeValue`
  (string, read this), `Value` (same), `IsVisible`, `IsEnabled`, `IsValid`, `Style` (control
  kind, for example "Textbox"), `Choices` (array of display texts, or null), `SupportingMetadata`.
  The panel holds only content type fields: no file name, no document id, no work item metadata.
- `metadataKvp`: the `panel` entry of the attached field.
- `builder` (each method returns builder; changes apply after the script ends):
  - `SetFieldIsVisible(field, bool)`, `SetFieldIsEnabled(field, bool)`
  - `SetFieldRuntimeValue(field, value, suppressBehaviors = false)`
  - `SetFieldIsValid(field, bool)` (`true` also clears every message on the field)
  - `SetFieldValidationMessage(field, text)`
  - `SetFieldChoices(field, arrayOfStrings)` (each string is both display text and value)
  - Shorthands for the attached field: `SetIsVisible(bool)`, `SetIsEnabled(bool)`,
    `SetIsValid(bool)`, `SetValidationMessage(text)`, `SetRuntimeValue(value)` (suppresses).
  - Aliases: `SetFilterIsVisible`, `SetFilterValue` (suppresses), `SetFilterIsValid`,
    `SetFilterValidationMessage`.
- `context`: `username`, `usergroups` (array of group names), `app`. All lowercase.
  Browser `app` values: webindex, webcapture, upload, addins, view, qc, search, interactions,
  taxonomy (the editor's Test preview). Server: username "", usergroups [], app "worker".
- `helper.GenerateUUID()`: a random UUID string. Not cryptographically secure.

## Rules
1. Every value is a string. Boolean reads "true" or "false"; compare lowercased. Numbers:
   parseFloat after removing "$" and ",". Dates: ISO 8601 in UTC; use getUTC... methods.
2. Field names are exact and case-sensitive, spaces included. A wrong name in `panel[...]`
   throws. A wrong name in a builder call fails the behavior with nothing in the console.
3. A behavior reacts to its own field. If the result depends on two fields, the same script goes
   on both fields. Say so in your answer.
4. Writes apply after the script ends. Reads in the same run see old values. For each method and
   field, only the last call counts.
5. `SetFieldRuntimeValue` on another field fires that field's behaviors unless the third argument
   is true. Two scripts that set each other's fields without it loop until the timeout.
6. Validation: call `SetFieldIsValid(field, ok)` first, then `SetFieldValidationMessage(field,
   text)` only when not ok. NEVER pass "" as a message: it shows "Could not find localized
   string." Never put ";" in a message: it splits into lines.
7. All Custom Behaviors share one valid flag per field. Put each field's rules in one script.
8. NEVER use `builder.SetChoices`: it is broken. Use `builder.SetFieldChoices(field, list)`.
9. Pass real booleans to SetFieldIsVisible, SetFieldIsEnabled, SetFieldIsValid. Pass strings to
   SetFieldRuntimeValue (`total.toFixed(2)`, "" to clear).
10. The script runs on every panel load and on the server. A script that stamps a value must
    check that the target is empty. Skip user-dependent code when `context.app === "worker"`.
11. On the server, a field marked invalid stops the document from being marked as indexed. In
    Start QC Task that means the work item goes to a person. Decide what the server should
    conclude, and write the script for it.
12. Hiding is not security. Anything that must be enforced or run unattended belongs in a Custom
    Step or the workflow.
13. Write plain ES5 style: `var`, `function`. Never use `console`, `Context`, `logger`, `http`.

## Example: stamp the approving user
Attach to: Approved (Boolean). Reads Approved, ApprovedBy. Writes ApprovedBy (Textbox).

```javascript
var approved = (panel["Approved"].RuntimeValue || "").toString().toLowerCase() === "true";
var approvedBy = (panel["ApprovedBy"].RuntimeValue || "").toString();

if (context.app !== "worker") {
    if (approved && approvedBy === "" && context.username) {
        builder.SetFieldRuntimeValue("ApprovedBy", context.username, true);
    }
    if (!approved && approvedBy !== "") {
        builder.SetFieldRuntimeValue("ApprovedBy", "", true);
    }
}
```

## Output
- One script. Start it with a comment block: the field it attaches to, the fields it reads, the
  fields it writes, what the user sees.
- Then tell the user how to attach it: Connections app > content type > field > Behaviors >
  Add a Behavior > Custom Behavior; name it, paste, select Test, then Save & Close. If the field
  has other behaviors, say where it goes in Change Behavior Order. List every field that needs a
  copy of the script.
- Say what the script does on the server runs.
- If you do not know a field's exact name or control type, ask. Do not guess names.