> For the complete documentation index, see [llms.txt](https://codemash.gitbook.io/codemash/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codemash.gitbook.io/codemash/cloud/database/collections/schema-triggers.md).

# Schema triggers

Create a schema trigger — send an email, push or SMS, call a webhook, push a realtime event or run code when a record is inserted, updated or deleted.

**Where:** Database → Collections → row menu → **Edit schema** → **Triggers** tab → **Add New** (or the flame icon in the Collections list) · `/projects/<project>/db/schemas/<schemaName>/triggers/new`

A **schema trigger** runs an action every time a record of one collection is inserted, updated or deleted — no matter which app, script or AI tool made the change. The rules that are the same in every module are on the [Triggers](/codemash/cloud/concepts/triggers.md) concept page.

## Before you start

* The collection exists and is published — see [Create a schema](/codemash/cloud/database/collections/create-schema.md).
* For **Call Webhook**: a webhook destination subscribed to the matching event (`database.record.inserted`, `…updated`, `…deleted`) in [Project → Settings → Webhooks](/codemash/cloud/project/settings/webhooks.md). For **Send Email / Push / SMS**: an integration and a template of that channel, for example an [email template](/codemash/cloud/emails/templates.md).
* You need `database:update` on the schema's triggers (`database:trigger:<schemaId>`).

## Steps

1. Open the collection's schema and switch to the **Triggers** tab. The list is titled **Database Triggers**. Press **Add New**.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-577d4ca27f19760bb039ebcbb82347d00234d6bb%2Fschema-triggers-1.png?alt=media" alt="Editing books schema, Triggers tab: Database Triggers with an empty list and the Add New button"><figcaption><p>The Triggers tab of the books schema.</p></figcaption></figure>
2. Under **If this** → **Select trigger actions**, pick the record event: **When inserted**, **When updated** or **When deleted**. **Add activation code** lets you add code that runs first and can stop the trigger.
3. Under **... then**, open the action picker and choose the action.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-e62e988c6c399afb2f46fea444cfdf1ec8ee8a82%2Fschema-triggers-2.png?alt=media" alt="Add New Trigger: When inserted is selected; the action list is open with Run Code, Send Email, Send Push Notification, Send SMS, Call Webhook and Send In-App (Realtime / SSE)"><figcaption><p>The action picker.</p></figcaption></figure>

   | Action                                                     | What it does                                                                                                                                                  |
   | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
   | **Run Code**                                               | Runs an installed marketplace function with the event data.                                                                                                   |
   | **Send Email** / **Send Push Notification** / **Send SMS** | Sends a template to the recipients you choose. The record's fields reach the template as tokens (`@Model.title`); an update also carries the previous values. |
   | **Call Webhook**                                           | POSTs a signed JSON envelope to the destinations you tick. Only destinations subscribed to the event can be picked; **Save** stays off until you tick one.    |
   | **Send In-App (Realtime / SSE)**                           | Delivers the event live to connected apps or AI agents.                                                                                                       |
4. Fill the action's part of the form, then the trigger's own fields:

   | Field                                       | Required | What it does                                                                | Example                                          |
   | ------------------------------------------- | -------- | --------------------------------------------------------------------------- | ------------------------------------------------ |
   | Send to webhook destinations (Call Webhook) | yes      | The destinations that get the call.                                         | `Fulfilment service` (Subscribed)                |
   | Order                                       | no       | Run order when several triggers listen to the same event; lower runs first. | `2`                                              |
   | Name the trigger                            | yes      | The name in the list.                                                       | `Notify fulfilment about a new book`             |
   | Description                                 | no       | Free text.                                                                  | `Sends every new book to the fulfilment service` |
   | Is this trigger enabled?                    | no       | On: the trigger runs. Off: saved but quiet.                                 | on                                               |

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-8b27a71c11d598bc1dbfe1228afd485bd445bef1%2Fschema-triggers-3.png?alt=media" alt="Lower part of the trigger form: Send to webhook destinations with Fulfilment service (Subscribed, https://hooks.example.com/norbix/orders) ticked, Order empty, Name the trigger Notify fulfilment about a new book, Description, the switch Is this trigger enabled?, Cancel and Save"><figcaption><p>A Call Webhook trigger, filled in.</p></figcaption></figure>
5. Press **Save**.

{% hint style="warning" %}
A new trigger starts **enabled**. Today the **Is this trigger enabled?** switch is still drawn in the off position on a new trigger; that is a display fault — a trigger saved without touching the switch is saved **enabled**. Check the **Enabled** column after you save, and disable the trigger from the **Actions** menu if you did not want it to run yet.
{% endhint %}

## Result

The trigger is in the schema's **Triggers** list with **When**, **What**, **Environment** and **Enabled**, and the collection shows the flame icon on **Database → Collections**. The next matching record change runs the action — here, every new book is POSTed to the fulfilment service.

<figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-bbad530721fa230da210fa08687a35790da02130%2Fschema-triggers-4.png?alt=media" alt="Database Triggers list with one row: Notify fulfilment about a new book, When inserted, Call Webhook, PROD, Enabled Yes"><figcaption><p>The saved trigger in the list.</p></figcaption></figure>

* Each trigger fires only for its own event and only in its own environment. Triggers reach PROD by [promotion](/codemash/cloud/project/settings/environments.md#promote-content-up-the-ladder).
* A destination that is subscribed to the event **and** chosen on a Call Webhook trigger gets two deliveries for one change: the plain one and the trigger one. Both carry the same `eventId` — de-duplicate on it, see [Webhooks](/codemash/cloud/project/settings/webhooks.md#what-a-delivery-looks-like).
* An Email / Push / SMS run that cannot send marks the trigger **Needs attention** in the list.
* Old triggers with the legacy **Code** action show **Code (not run)** in the list: they are saved but never run.

## Change or remove it later

Click the trigger's name in the list. The form opens as **Edit Trigger**, with the trigger id (`trg_1p4R3gTobGZpD6fXvHT4rd`) and an **Actions** menu.

<figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-5a02530fd47dcf3d9328f5b3e18962f59e684e95%2Fschema-triggers-5.png?alt=media" alt="Edit Trigger with the id trg_1p4R3gTobGZpD6fXvHT4rd and the open Actions menu: Disable, Delete trigger..., See documentation"><figcaption><p>Edit Trigger → Actions.</p></figcaption></figure>

* **Edit** — change any field and press **Save**. A trigger cannot move to another schema.
* **Disable** / **Enable** — **Actions** → **Disable** keeps the trigger but stops it; a disabled trigger says *Trigger is disabled, but you can still edit it.*
* **Delete** — **Actions** → **Delete trigger...**, then **Delete** in the confirmation (*Are you sure you want to delete?*). A schema with triggers cannot be deleted until its triggers are gone.

## Do it from code

```ts
const schemaId = 'sch_7lPCzWonEvTQsheGDCaKq'; // books
const { list } = await norbix.hub.database.getSchemaTriggers({ schemaId });
const trigger = list.items[0];
console.log(trigger.name, trigger.type, trigger.actionType, trigger.isEnabled);
// Notify fulfilment about a new book OnInserted WebhookCall true
await norbix.hub.database.disableSchemaTrigger({ schemaId, triggerId: trigger.viewId });
await norbix.hub.database.enableSchemaTrigger({ schemaId, triggerId: trigger.viewId });
```

```sh
norbix hub database schema triggers get --schemaId sch_7lPCzWonEvTQsheGDCaKq
```

To create one from code, send `saveSchemaTrigger` with `type: 'Schema'`, `schemaId`, `name`, `when` (`OnInserted`, `OnUpdated`, `OnDeleted`) and `action`. The client setup is on the [Database page](/codemash/cloud/database.md#use-it-from-code). API: [Save Schema Trigger](/codemash/api-reference/database/schemas/save-schema-trigger.md) · [Get Schema Triggers](/codemash/api-reference/database/schemas/get-schema-triggers.md) · [Enable](/codemash/api-reference/database/schemas/enable-schema-trigger.md) · [Disable](/codemash/api-reference/database/schemas/disable-schema-trigger.md) · [Delete](/codemash/api-reference/database/schemas/delete-schema-trigger.md)
