Triggers
Triggers
Trigger actions allow to execute a script whenever a data record is created, updated, or deleted. This is very similar in concept to database triggers, with the added flexibility of Stryke scripts. They are very useful for automating processes when data created or changed, performing validations, data manipulation or augmenting data with other records or information coming from external services.
There are 6 different types of triggers:
Operation | Phase | Description |
---|---|---|
Create | Before | Executes a script just before the record is created |
Create | After | Executes a script just after the record is created |
Update | Before | Executes a script just before the record is update |
Update | After | Executes a script just after the record is update |
Delete | Before | Executes a script just before the record is deleted |
Delete | After | Executes a script just after the record is deleted |
Below is a full list of all types of triggers available in Stryke with all the implementation details and restrictions.
Before Create Trigger
A before create trigger runs every time one or more records of the chosen entity are created, just before the new records are saved. In the script's source code, the records being created can be accessed through the Stryke Library as follows:
stryke.data.new
: the records that are being created
stryke.data.new
Always holds an array. This allows passing to the trigger code all records that are being created in a single operation. For example when the data create APi is called with multiple records or when importing records from CSV. When creating a single record through the API, UI or via scripts stryke.data.new
will still hold an array with just a single element.
We recommend always implementing trigger scripts taking into consideration that more than one record can be created in a single operation and hence "bulkifying" the trigger code.
All "Before create" trigger scripts must end with a call to stryke.resolve()
with the records to be created passed as arguments. The objects passed to the resolve function will be used as the data of the records being created.
Example:
stryke.resolve(stryke.data.new);
After Create Trigger
An after create trigger runs every time one or more records of the chosen entity are created, just after the new records have been saved to the database. In the script's source code, the records being created can be accessed through the Stryke Library as follows:
stryke.data.new
: the records that are being created
"After create" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
Before Update Trigger
A before update trigger runs every time a single record of the chosen entity is updated, just before the record is modified. Note that Stryke does not support batch update requests yet.
In the script's source code, the record being updated can be accessed through the Stryke Library as follows:
stryke.data.old
: the record that is being updated, before any update is applied
stryke.data.new
: the record that is being updated, with the updated values
"Before update" trigger scripts must return the record to be modified, in the state in which it is to be saved,
using the stryke.resolve()
function. The value passed to the resolve function will be used as the data of the record being updated.
After Update Trigger
An after update trigger runs every time a single record of the chosen entity is updated, just after the record is modified. Note that Stryke does not support batch update requests yet.
In the script's source code, the record being updated can be accessed through the Stryke Library as follows:
stryke.data.old
: the record that is being updated, before any update is applied
stryke.data.new
: the record that is being updated, with the updated values
"After update" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
Before Delete Trigger
A before delete trigger runs every time a single record of the chosen entity is deleted, just before that record is deleted. Note that Stryke does not support batch delete requests yet.
In the script's source code, the record being deleted can be accessed through the Stryke Library as follows:
stryke.data.old
: the record that is about to be deleted
"Before delete" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
After Delete Trigger
An after delete trigger runs every time a single record of the chosen entity is deleted, just after that record is deleted. Note that Stryke does not support batch delete requests yet.
In the script's source code, the record being deleted can be accessed through the Stryke Library as follows:
stryke.data.old
: the record that is about to be deleted
"After delete" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
Nested executions
When you create a trigger you are defining code that will be executed automatically under certain circumstances. As a result of this it is possible that a script causes another script to execute. For example, if you create a record from a script and that record has a create trigger or if you create a record that has a "create trigger" from the update trigger of another record. We call these scenarios "nested executions". Nested executions are supported, but only up to 3 levels of depth. If your app has more that 3 levels of scripts being executed within the same request, an error will be thrown.
Error handling
If an error of any kind occurs in a script, all the changes made in that request will be rolled back.
Imagine the following scenario:
- edits a record (record 1)
- creates a second record (record 2)
- record 2 has a create trigger
- the create trigger is executed and causes an error
All changes done within this request, including the initial edit of record 1, will be discarded.
Bear in mind that any error that you return from your scripts using stryke.error()
will be returned in the response and will be visible by end users.