System, flow control, and AI
Learn how to pause scripts, inspect the app execution context.
System and flow control functions help you manage timing, sync, caching, and environment-specific behavior. You can pause a script, inspect raw values, and detect app context.
In this chapter, you will learn how to:
Control timing in a running script.
Inspect the current app environment and database status.
eval()
Execute text as a script
generateObject()
Get JSON object data from Ninox AI
generateText()
Get text data from Ninox AI
ninoxApp()
Return the current client or app context
raw()
Return the internal text representation
sleep()
Pause a script briefly
Control timing and execution order
Use these functions when one action should happen only after another has completed.
Pause a script briefly
Use sleep() to force Ninox to wait for a given number of milliseconds before the script continues.
Use it when you want to:
Add a short delay before a follow-up action.
Avoid timing issues in a UI flow.
Wait briefly for another process to settle.
Show a note or warning before the next step runs.
sleep(number)
numberthe delay in milliseconds
sleep() returns no value.
Let’s take a look at some examples:
Pauses the script for 2 seconds.
Tips:
Keep delays as short as possible.
Use a delay only when the flow really needs it.
Inspect raw values and range boundaries
Use these functions when you need the underlying value or the start boundary of a duration or range context.
Inspect the raw value
Use raw() to return the internal raw text representation of a value.
Use it when you want to:
Convert formatted text into its raw text form.
Read the internal value behind a color field.
Reuse formatted content in dialogs or emails.
Compare the raw form with the displayed form.
raw(any)
anythe value you want in raw form
raw() returns a string.
Let’s take a look at some examples:
Returns the content of a formatted text value as unformatted text with HTML tags.
Displays a dialog with the contents of the formatted text field Dialog text.
Returns the internal color value, for example #EC87E2.
Tips:
Use
raw()when you need the system's internal text representation.This is useful for formatted text fields, dialogs, and emails.
You can format the result again later with
html().
Execute strings as formulas and inspect data with eval()
Use eval() to execute text as a script.
This lets you create dynamic expressions. For example, use table, field, and variable names that change at runtime.
eval(string, nid)
stringtext containing calculations or a Ninox scriptnidthe record to which the script applies
Let’s take a look at some examples:
Returns 20 if the current record's "Manual formula" field contains "14 + 6 * 0.5 * 2".
Returns the sum of all amounts in the "Invoices" table.
Shows an alert with the message "This is a test".
Returns the value "Toni Kim" from the "Full name" field. For a choice field, it returns the value's index.
Returns the text value of a choice field.
Returns the number of records in the "Contacts" table whose "Full name" field matches the selected field value in the current record. Use this approach for dynamic duplicate searches when myField is selected from a choice field.
Detect app context and database state
Use these functions when your script should react to the current client or the protection status of the database.
Detect the current app with ninoxApp()
Use ninoxApp() to check where your script is executed.
Use it when you want to:
Adjust behavior by client.
Diagnose client-specific workflows.
ninoxApp()
ninoxApp() returns a string that identifies the current environment.
Possible return values include:
clientserver
Let’s take a look at some examples:
Returns a string that identifies where the script is running.
Tips:
Use
ninoxApp()to check if your UI-based functions can be executed, which is not the case if the result is "server".
Let Ninox AI execute tasks with generateObject() and generateText()
Use these two functions to send data and instructions to Ninox AI and get text or a JSON object. Use them to classify text, extract values, or fill fields automatically.
generateObject(prompt, format), returns a JSON object with the fields format asks for.
generateObject(prompt, format, model)
generateText(prompt), returns text.
generateText(prompt, model)
promptis a text value or an array of texts. It contains the data Ninox AI should inspect and the instruction for Ninox AI.formatJSON values to define the returned fields and their types:"string","number","boolean","date", or a list of text values when Ninox AI must select one option. Provide them in curly braces.modelis a JSON value to choose an AI model:"haiku"(default),"sonnet","opus", or"luna". Provide it in curly braces.
If you refer to Ninox fields, their raw values are taken, for example, a choice's option ID instead of its label. Use the text() function to get the text value instead.
Read the result in format with r.field, item(r, "field"), or for key, value in r do … end.
A date returns text in YYYY-MM-DD format. Use date() to convert it to a date value.
Note the following limits:
formatfields: maximum 20. Use 1-7 where possible.Choices per list: 2-50. Use up to 10 where possible.
Field names: letters, digits, and
_only. Start with a letter.erroris reserved.promptsize: maximum 20,000 charactersTimeout: 25 seconds
AI calls per script run: maximum 50
Up to four calls can run at once per workspace.
Let’s take a look at some examples:
Classifies a support ticket and fills the category and priority fields.
Extracts a number and date from an email. The returned number can be used in calculations.
Displays a short summary of the received email in a pop-up message, using its subject and body.
Process many records
You can call the functions once for each record or once for a group of records. Choose the approach based on the result you need.
Use an AI call for each record when you need to fill a field for every record. This example classifies each record in the "Tickets" table:
Five records result in five AI calls. Each record receives its own category.
Use one AI call for all records when you need an overview or report for multiple records. This example sends all ticket titles and messages in one AI call:
Five records result in one AI call. This approach is faster and uses fewer AI credits.
Tips:
Each AI call uses AI credits.
Send only necessary data to the Ninox AI.
No prompt, message or answer content is logged.
The result contains the fields defined in
format. Unknown values arenull.On failure, both functions return
{error: "message"}. Checkr.errorbefore using the result.
Last updated
Was this helpful?