> 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/sending-an-email-upon-saving-a-case.md).

# Sending an Email Upon Saving a Case

The most common on save script sends an email when something on the case changes. The shape is simple: a trigger line naming the field, an `on_save` that allows the save through, and an `after_save` that names a template and tells the user what happened.

Putting the send in `after_save` matters whenever the template quotes the field that triggered it. Because `on_save` runs before the new values are written, a template rendered there would still show what was stored before the user's edit.

## The scenario

A charity records that a volunteer's DBS check has cleared. The volunteer coordinator should be told automatically.

| Setting       | Value                             |
| ------------- | --------------------------------- |
| Trigger field | `5101` DBS check cleared (date)   |
| Template      | `VolunteerDBSCleared-Coordinator` |

## The script

```javascript
[Changed.CustomField(5101).Changed]

function on_save() {
    return true;
}

function after_save() {
    Service.SendEmail("VolunteerDBSCleared-Coordinator");
    Service.AddMessage("The volunteer coordinator has been told the DBS check is clear.", "notify-success");
    return true;
}
```

## What each part does

The trigger fires only when field `5101` actually changes, so opening and re-saving a case without touching that field does nothing.

`on_save` returning `true` is all that is required to let the save proceed. There is no validation here, so the function could be omitted entirely and the script would still run its `after_save`. Keeping it makes the intent obvious when you add conditions later.

`SendEmail` takes the **email template name**. Who receives it, and what the message says, come from the template's own settings, not from the script. The script's only job is to decide when to send.

## Adding a condition

Often you only want the email when the new value means something specific:

```javascript
function after_save() {
    if (Changed.CustomField("5101").NewValueAsText === "") {
        Service.AddMessage("The DBS date was cleared, so no email was sent.", "notify-info");
        return true;
    }

    Service.SendEmail("VolunteerDBSCleared-Coordinator");
    Service.AddMessage("The volunteer coordinator has been told the DBS check is clear.", "notify-success");
    return true;
}
```

Clearing a date still counts as a change and still triggers the script, so handling the empty case explicitly stops a notification going out about a check that was just removed.

## What to watch for

If nothing arrives, check the template name character for character, confirm the template is attached to this case type, and look at the case's communications tab for a failed send. The script itself gives no error when a template name does not match anything.

{% hint style="info" %}
The same pattern works for any field type. [Email When a File Is Uploaded](/developer-documentation/case-scripting/on-save-scripts/examples/email-when-a-file-is-uploaded.md) shows it triggered by a file upload rather than a date.
{% 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/sending-an-email-upon-saving-a-case.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.
