Action Definitions (Server Functions)
Action Definitions (Server Functions)
Actions are custom Server-Side Functions attached to an object. They allow you to encapsulate complex business logic that goes beyond standard CRUD operations.
Unlike Hooks (which trigger automatically), Actions are explicitly invoked by the client (API, Button, Scheduled Task).
1. Concepts
1.1 Scope (type)
- Global Actions: Operate on the collection level.
- Examples: "Import CSV", "Generate Monthly Report", "Sync with External API".
- Context: No
id.
- Record Actions: Operate on a specific record instance.
- Examples: "Approve", "Reject", "Send Email", "Clone".
- Context: Has
id.
1.2 Schema-First Inputs
Input parameters (params) are defined using the same FieldConfig schema as object fields. This gives you free validation, type coercion, and UI generation.
2. Configuration (YAML)
Actions are declared in your object definition file (<object_name>.object.yml).
3. Implementation (TypeScript)
Implement the logic in a companion <object_name>.action.ts file.
File Naming Convention: <object_name>.action.ts
The filename (without .action.ts) must match your object name to enable automatic binding.
Examples:
order.action.ts→ Actions fororderobjectproject.action.ts→ Actions forprojectobject
4. Why this design is "Optimal"?
- Unified Schema: Inputs use the same definitions as Database fields. If you know how to define a table, you know how to define an API argument.
- UI Ready: The metadata (
label,icon,confirm_text,params) contains everything a frontend framework (like React Admin or Salesforce Lightning) needs to render a button and a modal form automatically. - Type Safety: The
ActionDefinition<Entity, Input, Output>generic ensures your handler code respects the contract.
5. Loading & Registration (Standard)
To ensure the Metadata Loader can automatically bind your actions to the correct object, you must follow the file naming convention:
- Object Definition:
mypackage/objects/invoice.object.yml - Action Implementation:
mypackage/objects/invoice.action.ts(or.js)
The loader extracts the objectName from the filename (everything before .action.).
The loader will register approve_invoice and reject_invoice as actions for the invoice object.