Overview
An event is something your app declares once and emits from a logic function each time it happens.Beta. Events can be declared, emitted, checked against their definition and used to trigger your app’s logic functions and agents, and any workflow of the workspace. Realtime delivery and storage are planned.
Declaring an event
An event describes its properties with the same schema format as a tool trigger’sinputSchema: an object schema, with type: 'record' and objectUniversalIdentifier for a reference to a record.
src/events/message-added.event.ts
name and property names are camelCase. An event name is unique within your app. Dotted names are reserved for events Twenty itself emits.
Properties are flat: nested objects and arrays are refused.
enum, minimum and maximum are checked too. label and description document a property.
Emitting
src/logic-functions/handlers/post-message-handler.ts
event or with eventUniversalIdentifier: MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER, not both. Either way, only events your app declares can be emitted.
id is optional. Set it whenever the emission may be retried, such as by a retried logic function or after emitEvent throws, since a refused emission may already have reached some receivers. Both emissions then carry the same id and each receiver runs once. It follows the same rules as a background job’s jobId: letters, digits, _, . and -, up to 128 characters. Without it, Twenty generates one.
The server checks the properties against the schema and refuses anything undeclared or mistyped. It returns the occurrence id and the time it was recorded.
Triggering a logic function
A logic function of your app runs each time one of your app’s events is emitted when itseventTriggerSettings names the event:
src/logic-functions/on-message-added.ts
- The function receives the occurrence:
{ id, type, occurredAt, properties }. - Only events your app declares can trigger its functions; syncing a trigger on any other event fails.
- The run is queued like a background job and follows the function’s
retryLimit. It has no member: it acts with the app’s own token, like a cron run. - When Twenty cannot queue the runs, for instance once your app hits its job rate limit,
emitEventfails with the reason.
Triggering an agent
An agent of your app runs each time one of your app’s events is emitted when one of its triggers names the event:src/agents/reply-assistant.ts
- The run receives the event type, the time it occurred and its properties.
- Only events your app declares can trigger its agents; syncing a trigger on any other event fails.
- Each agent’s triggers share its run limit. Once it is reached, the agent skips the occurrence and
emitEventstill succeeds.
Triggering a workflow
Every event your app declares shows up in the workflow builder, under Apps in the trigger list, labelled with itslabel. A workspace member picks it as the trigger of a workflow, and each emitted occurrence starts a run.
- The steps read the occurrence as
{{trigger.id}},{{trigger.occurredAt}}and{{trigger.properties.<name>}}. The variable picker lists the properties from the event’sschema, with theirlabel. - Your app does not know which workflows listen. Put in
propertiesonly what any member who builds a workflow may see; prefer record ids to content, so workflows read records through their own permissions. - A run that cannot be queued is recorded as a failed run of that workflow, and
emitEventstill succeeds.