Triggers
Fields of a trigger, which runs a server script before or after a record is created, updated or deleted, with two worked examples.
A trigger runs a server script when a record on its table is created, updated or deleted, for example to set a field from another one. This page lists a trigger’s fields for administrators and developers who write them. You need the administrator role and JavaScript knowledge.
Triggers are under Administration → Business Logic → Triggers. The script runs on the server and can use any script library. See Where scripts run.
Trigger fields
| Field | Type | Behaviour | Purpose |
|---|---|---|---|
| Trigger name | Text | Required. | Identifies the trigger, for example “Incident - Location based on Requested for location”. |
| Table | Table | Required. | The trigger runs for records on this table. |
| Field | Field | Optional. When set, the trigger runs only if this field changed. | Limits the trigger to one field’s changes. |
| Description | Text | Optional. | Says what the trigger does, for other administrators. |
| Active | Yes or no | Default yes. | Only active triggers run. |
| Trigger when? | Choice | Required. Default Before. Values are in the next table. | When the script runs relative to the save. |
| On transition(s) | Workflow transitions | Optional. | Runs the trigger only when the record moves along one of these transitions. |
| On create | Yes or no | Runs when a record is created. | |
| On update | Yes or no | Runs when a record is updated. | |
| On delete | Yes or no | Runs when a record is deleted. | |
| Order | Number | Default 10. Lower numbers run first. | Orders triggers of the same table and timing. A later trigger sees field values an earlier one set. |
| Stop on exception | Yes or no | If this trigger fails, later triggers do not run. If a Before trigger fails, no After trigger runs either. | Stops dependent logic after an error, such as a syntax error or a missing field. |
| Overrides | Trigger | Optional. The named trigger, on a parent table, no longer runs for this table. | Replaces inherited logic, for example a Work trigger for Incident only. |
| Script | Script | Required. Receives current, previous and operation. |
The logic to run. |
Trigger when values
| Value | Runs | Use it to |
|---|---|---|
| Before | Before the record is written to the database. | Set or check fields on the record itself. You do not need current.update(). |
| After | After the record is written. | Create or change other records, or call an integration. |
| Async | In the background after the save, through the async.trigger system event. |
Work that need not finish before the user sees the save. |
| Clone | While a record is being cloned. The script receives the new copy as current and the original as source. |
Adjust or clear fields on a copy. |
| Export record | While a record is being written to an XML export. Changes appear in the export but are not saved. | Change what an export contains. |
On transition(s), On create, On update, On delete and Field do not apply to Clone and Export record triggers.
Example: assign an incident to the user who starts work
This Before trigger runs on the transition from New to Work in progress. It sets the assignment group to one of the user’s groups and the assignee to the user.
| Field | Value |
|---|---|
| Table | Incident |
| Trigger when? | Before |
| On transition(s) | The transition from New to Work in progress in the incident workflow |
| On create | No |
| On update | Yes |
| On delete | No |
// Use the current group if the user is a member, otherwise the user's first selectable group
let groupToAssign = "";
let currentGroup = current.AssignmentGroup.value();
let aGroupRec = Table("Group")
.AND(
EQUAL("Selectable", true),
OR(
SUBQUERY("AccessibleFrom", IN("Table", ["Incident"])),
NOT(SUBQUERY("AccessibleFrom"))
),
SUBQUERY("Member", EQUAL("ID", user.getID()))
)
.query();
while (aGroupRec.next()) {
let currentGroupID = aGroupRec.getID();
if (currentGroupID == currentGroup) {
groupToAssign = currentGroupID;
break;
} else if (groupToAssign == "") {
groupToAssign = currentGroupID;
}
}
if (groupToAssign != "") {
current.AssignmentGroup(groupToAssign);
}
current.Assignee(user.getID());
Example: send an incident to another ticketing system
This After trigger calls a script library to send the incident to another system. The other system’s ticket ID is kept in the ThirdPartyReference field. The example assumes a script library named IncidentIntegrationUtil with a function synchToOtherSystem. To write the HTTP call, see HTTP.
| Field | Value |
|---|---|
| Table | Incident |
| Trigger when? | After |
| On create | Yes |
| On update | Yes |
| On delete | No |
let action = current.ThirdPartyReference.hasValue() ? "update" : "create";
let returnMessage = IncidentIntegrationUtil.synchToOtherSystem(action, current);
current.WorkJournal(returnMessage);
// Write only the journal entry: do not run the triggers again
current.disableTriggers();
current.update();