> For the complete documentation index, see [llms.txt](https://docs.agilecase.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.agilecase.com/developer-documentation/case-scripting/on-save-scripts/examples.md).

# Examples

Each page below is a complete on save script with the settings it depends on, written around a different technique. Between them they cover every common shape an on save script takes: sending things out, writing to other fields, refusing a save, reacting to a table, and calling another system.

The scenarios are drawn from several sectors deliberately. An on save script is not specific to any kind of casework, and the same handful of patterns solve the same problems whether you are running a charity, a school, an insurance desk or a membership club.

## Finding an example

Search this table for the method you want to use, the field type you are triggering from, or the outcome you are after. The Keywords column is plain text, so your browser's find-on-page search works across it.

| Example                                                                                                                                            | Level        | What it does                                                             | Keywords                                                                                                   |
| -------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| [Sending an Email Upon Saving a Case](/developer-documentation/case-scripting/on-save-scripts/examples/sending-an-email-upon-saving-a-case.md)     | Beginner     | Sends an email template when a field changes                             | email, SendEmail, template, date field, notify, after\_save, charity, DBS                                  |
| [Email When a File Is Uploaded](/developer-documentation/case-scripting/on-save-scripts/examples/email-when-a-file-is-uploaded.md)                 | Beginner     | Sends an email when a document lands in an upload field                  | email, SendEmail, file upload, document, attachment, insurance, report                                     |
| [Reacting to Old and New Values](/developer-documentation/case-scripting/on-save-scripts/examples/reacting-to-old-and-new-values.md)               | Beginner     | Acts on how a value changed, not just what it became                     | OldValueAsText, NewValueAsText, transition, copy a field, clear a field, consent, Yes to No, club, charity |
| [Update Case Information](/developer-documentation/case-scripting/on-save-scripts/examples/update-case-information.md)                             | Beginner     | Changes the case status or a milestone date                              | UpdateCaseInfo, status, Open, Closed, Pending, milestone, fee earner                                       |
| [Add Task](/developer-documentation/case-scripting/on-save-scripts/examples/add-task.md)                                                           | Beginner     | Creates a task and assigns it to someone                                 | AddTask, assign, fee earner, due date, follow up, review                                                   |
| [An Action Dropdown That Resets Itself](/developer-documentation/case-scripting/on-save-scripts/examples/action-dropdown-that-resets-itself.md)    | Intermediate | Uses a dropdown as a button, then clears it so it can be used again      | dropdown, action, command, Please Select, reset, re-run, privacy notice                                    |
| [Banding a Number Into a Value](/developer-documentation/case-scripting/on-save-scripts/examples/banding-a-number-into-a-value.md)                 | Intermediate | Maps an amount onto a band, writes it, and alerts above a threshold      | NewValueAsNumber, parseFloat, switch, bands, threshold, excess, premium, insurance                         |
| [Mapping a Status Onto Other Fields](/developer-documentation/case-scripting/on-save-scripts/examples/mapping-a-status-onto-other-fields.md)       | Intermediate | Sets several flags and stamps a date from one status change              | status, dropdown, flags, date stamp, enrolled, withdrawn, school, sibling                                  |
| [Sending an SMS With an Email Fallback](/developer-documentation/case-scripting/on-save-scripts/examples/sending-an-sms-with-an-email-fallback.md) | Intermediate | Texts a contact, or emails the office when there is no usable number     | SendSMS, SMS template, mobile, 07, validation, fallback, absence, school                                   |
| [Calling a Webhook on a Milestone](/developer-documentation/case-scripting/on-save-scripts/examples/calling-a-webhook-on-a-milestone.md)           | Intermediate | Pushes an event to another system when a date is set                     | SendWebhook, integration, finance, accounts, milestone, grant, charity                                     |
| [Triggering From a Table Row](/developer-documentation/case-scripting/on-save-scripts/examples/triggering-from-a-table-row.md)                     | Intermediate | Fires when a cell in a custom field table reaches a value                | table trigger, Table., custom field group, row, column, booking, club                                      |
| [Blocking a Save With Validation](/developer-documentation/case-scripting/on-save-scripts/examples/blocking-a-save-with-validation.md)             | Intermediate | Refuses the save and tells the user why                                  | return false, validation, cancel save, block, DateDiff, consent, GDPR                                      |
| [Acknowledging a Privacy Notice](/developer-documentation/case-scripting/on-save-scripts/examples/acknowledging-a-privacy-notice.md)               | Advanced     | Submit on a form sends several emails and copies answers onto the record | form, submit, save for later, privacy, fan out, copy answers, received date                                |
| [Handling a Form Submission](/developer-documentation/case-scripting/on-save-scripts/examples/handling-a-form-submission.md)                       | Advanced     | Submit derives flags, stamps a date, and calls insurer webhooks          | form, submit, FNOL, questionnaire, webhook, senior review, insurance, orchestration                        |
| [Running an AI Action on a Document](/developer-documentation/case-scripting/on-save-scripts/examples/running-an-ai-action-on-a-document.md)       | Advanced     | Runs an AI template over an uploaded file and stores the result          | AIAction, AI, file upload, document, JSON, JSON.parse, GDPR, summary                                       |

### What the levels mean

| Level        | Typical contents                                                        |
| ------------ | ----------------------------------------------------------------------- |
| Beginner     | One trigger, one or two `Service` calls, little or no branching         |
| Intermediate | Several branches, a second field read, SMS, webhooks, or table triggers |
| Advanced     | Form submission, several outputs in one save, or AI over a document     |

## Before you copy an example

Every field ID in these pages, such as `5101` or `5302`, is made up. Field IDs are specific to your own system, so replace them with yours before using a script. The same is true of every template name.

Two things are worth knowing before you start, because they explain most of the choices made in these examples.

**Put anything you send in `after_save`.** Both functions queue their work and both only carry it out if they return `true`, so either one is safe. The difference is timing: `on_save` runs before the new field values are written, so an email sent from it renders your template against the values that were there before the user's edit. Sending from `after_save` means the template sees what was actually saved.

**Return `true` unless you mean to refuse the save.** Returning `false` from `on_save` abandons the save entirely, including the user's own edits, and drops everything the script had queued. That is exactly what you want for validation and nothing else. Returning `false` from `after_save` cannot undo a save that has already happened; it only discards that function's own queued work.

{% hint style="info" %}
For the full list of what a script can call, including methods these examples do not reach for such as `CompleteTask`, `AddTableRow` and `SendMessage`, see the [Script API Reference](/developer-documentation/case-scripting/on-save-scripts/script-api-reference.md).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.agilecase.com/developer-documentation/case-scripting/on-save-scripts/examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
