System events
Fields of system event definitions, system events and system event scripts, how events are processed, and how to raise one from a script.
A system event hands work to a background thread, so a save or an integration call does not wait for it. This page lists the records involved and shows how to raise an event, for administrators and developers with the administrator role.
Good candidates are audit actions, integration calls and batch calculations. The records are under Administration → Event configuration.
How events are processed
- A script or the platform creates a system event record with the event’s Name, in state Ready.
- The “Event processor - Default” job runs every five seconds. It takes up to 50 Ready events on the
defaultqueue, oldest first. - For each event it finds the system event definition with the same name. If there is none, the event is set to Error.
- It runs every active system event script for that definition, lowest Order first.
- The event is set to Processed if every script succeeded, or Error if one failed. Message holds the error.
Other queues have their own jobs, for example the global search queues.
System event definition fields
A definition documents an event and is what its scripts attach to.
| Field | Type | Behaviour | Purpose |
|---|---|---|---|
| Name | Text | Required. Events with this name use this definition. | Identifies the event, for example user.locked_out. |
| Description | Text | Says when the event is raised and what its parameters hold. | |
| Source | Text | “Built in” on the definitions Servicely ships. | Shows where the event comes from. |
System event fields
A system event records one occurrence.
| Field | Type | Behaviour | Purpose |
|---|---|---|---|
| Name | Text | Required. | The definition this event belongs to. |
| Queue | Text | Required. Default default, the shared queue. |
The queue that processes the event. A separate queue lets you pause one kind of work. |
| State | Choice | Ready, Processed or Error. | Where the event is in processing. |
| Table | Table | Optional. | The table of the record the event is about. |
| Identifier | Text | Optional. | The ID of the record the event is about. |
| Parameter 1 Name to Parameter 3 Name | Text | Optional. For readability only. | Names the parameters. |
| Parameter 1 to Parameter 3 | Text | Optional. Their meaning depends on the event. | Values the scripts read. |
| Payload | Script | Optional. | Passes state that does not fit the parameters. |
| Message | Text | Set when processing fails. | The error. |
| Processed on | Date and time | Set by the processor. | When the event was processed. |
| Duration | Number | Set by the processor, in milliseconds. | How long processing took. |
System event script fields
A system event script holds the logic for an event.
| Field | Type | Behaviour | Purpose |
|---|---|---|---|
| Name | Text | Required. | Identifies the script. |
| System event | System event definition | Required. | The event the script handles. |
| Active | Yes or no | Default yes. | Only active scripts run. |
| Order | Number | Default 10. Lower numbers run first. | Orders several scripts for one event. |
| Description | Text | Says what the script does. | |
| Table | Table | Optional. Used with Condition. | Limits the script to events about this table. |
| Condition | Condition | Optional. Tested against the record the event names. The script is skipped if it does not match. | Limits the script to some records. |
| Script | Script | Receives event, the system event record, and current, the record named by Table and Identifier. |
The logic to run. |
Raise an event from a script
First create a system event definition for the event, then create a system event record from any server script. This trigger script on Incident raises an event named incident.sync_requested for the record it runs on, with one named parameter. The name is an example. Choose your own.
Table("SystemEvent").newRecord()
.Name("incident.sync_requested")
.Table("Incident")
.Identifier(current.getID())
.Parameter1Name("action")
.Parameter1(operation)
.create();
A script for the event reads the parameter with event.Parameter1() and the incident with current.