Skip to main content
Version: Next

Custom Operations With Lua Scripts

A script operation (executable_script) is a Lua script that runs inside EP. It can read entities and change them — all in one atomic run. Nothing has to be deployed: the script is stored in EP like any other entity.

Create a Script Operation​

  1. Open Administration → Behaviors → Actions › Scripts. The search page lists all script operations.
  2. Click . Enter a name, for example mark_checked — letters, digits and _ only.
  3. Click Submit and open the new script operation.

Edit the Script​

  1. On the tab Data Sheet, find the field script. It shows the first line of the script.
  2. Click next to the field. The script editor opens with syntax highlighting and autocomplete for self.properties.* and self.relationships.*.
  3. Write the script and close the editor. The script is saved when the editor closes.

A first example — mark the entity as checked and report back:

self.properties["status_note"] = "checked"
return "Marked as checked"

The AI Low Code Generator next to the editor writes and explains scripts on request — for example "Set status_note to 'checked' and return a short confirmation."

No test run

The editor cannot run the script. Test it by running the action on a test entity.

Attach It as an Action​

  1. Open the entity type, tab Actions.
  2. Click Link existing, select the script operation and fill in name, label and icon of the action.
  3. Click Submit.

The action appears as a button on the entities of the type — see Actions. The same script can also run automatically as a hook on create, update or status change — see Lifecycle Hooks.

What the Script Can Do​

selfThe entity the action runs on. Read with self.properties["name"], self.relationships["has_address"].
Change valuesAssign to self.properties[...]. The change is written when the script ends.
Other entitiesents.load("type/key") loads any entity, get_entity(uuid) by id, ents.create("type", {...}) creates a new one. Relationship instances expose .target.
Return valueBecomes the result of the action. Only a string counts — any other value (number, table, nil) gives an empty result. A string containing JSON is passed on as JSON, so the same result formats as for service functions work, for example {"html_table": "<table>…</table>"}.
Not availableValues from an input mask (pre-mask), the user who started the action, the language of the user interface.
-- sum the weights of all ingredients and store the total
local total = 0
for _, rel in ipairs(self.relationships["has_ingredient"]) do
total = total + rel.target.properties["weight"]
end
self.properties["total_weight"] = total
return "Total weight: " .. total

Rules​

  • Atomic: everything the script changes is written together when it ends. If the script raises an error, nothing is written.
  • One commit per entity: assigning a value a thousand times still creates one commit.
  • Same checks as a normal save: read-only lifecycle statuses, branch rules, required fields and types apply.
  • Property values only: relationships, traits and lists cannot be changed by a script, and key and label cannot be set directly. But if the script changes a property that the key script or label script uses, the entity gets a new key or label — the old key then still points to the earlier commit.
  • No silent failures: an unknown property or a wrong type raises an error at once. There is no pcall.
  • Limits: a run may take at most 100 ms, and may change at most 32 aggregates (an entity together with what it owns).
  • Errors: if the script fails or runs too long, the user sees an error message; the details are only written to the server log.
No commit() call

You never save explicitly — there is no commit() function, and calling self:commit() fails the run. Changes to several entities in one run (for example self and an entity loaded with ents.load) are all written at the end, one commit per entity.

The complete rules are in Lua Scripting and Actions & Hooks.

Built-in Lua Packages​

For security reasons, scripts run in a sandbox: only a limited set of built-in Lua packages is available, and imports of other modules are not possible — require, io, os and pcall do not exist. Available are, among others, the basic functions (pairs, ipairs, type, tostring, tonumber, assert), the libraries math, string and table, and the EP helpers LangCode, RangeFloat and RichText. print writes to the server log.

A complete reference of all available packages and functions will follow in a future version of this documentation.