> 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/add-task.md).

# Add Task

An on save script can queue a task as part of saving. The task is not created as you call `AddTask`; it is queued and added only if the script returns `true`, in the same way as an email or a field update.

## The scenario

A customer rating on a case drops to fifty or below. The fee earner should receive a task to review the client, due in seven days.

| Setting       | Value                    |
| ------------- | ------------------------ |
| Trigger field | `6364` Customer rating   |
| Assigned to   | `Service.Case.FeeEarner` |
| Due           | 7 days from today        |

## The script

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

function on_save() {
    if (Changed.CustomField("6364").NewValueAsNumber <= 50) {
        Service.AddTask(
            "Review client rating",
            "Customer rating is 50 or below — please review",
            Service.Case.FeeEarner,
            7);
        Service.AddMessage("A review task has been added for the fee earner.", "notify-success");
        return true;
    }

    if (Changed.CustomField("6364").NewValueAsNumber < 100) {
        Service.AddTask(
            "Reinstate client",
            "Customer rating has improved — reinstate if appropriate",
            Service.Case.FeeEarner,
            7);
        Service.AddMessage("A reinstatement task has been added for the fee earner.", "notify-success");
        return true;
    }

    Service.AddMessage("No task was created because the rating is out of range.", "notify-error");
    return false;
}
```

## The AddTask arguments

```javascript
Service.AddTask(description, notes, assignedTo, dueDate)
```

Both `description` and `notes` are required. If either is empty, the task is dropped silently.

`assignedTo` can be a single user, such as `Service.Case.FeeEarner`, or a list built with `Service.Assignments` and `Service.UserCode` / `Service.GroupCode` when several people or a group should receive it.

`dueDate` takes either a number of days from today, such as `7`, or an exact date as `yyyyMMdd`, such as `"20260401"`. A negative number of days is rejected and the task is dropped.

See the [Script API Reference](/developer-documentation/case-scripting/on-save-scripts/script-api-reference.md) for the full set of overloads, including placing the task on a particular task list.

## Returning false for an invalid value

The final branch returns `false`, which abandons the save. That is appropriate here because the rating field itself is invalid, not because the task failed. The user sees the error and their edit is not stored.

If the save should proceed regardless, return `true` and show the message instead.

{% hint style="warning" %}
`NewValueAsNumber` handles whole numbers only. A rating of `12.5` reads as `0`, not `12`. For decimals, read `ValueAsText` and convert with `parseFloat`.
{% endhint %}

## Task created

<figure><img src="/files/QiQ6cq2ONXk3K8tzqlYW" alt=""><figcaption></figcaption></figure>

When the value of the field is changed and the case is saved, the notification message lets you know that the task has been added.

## Tasks in case

<figure><img src="/files/OIJfIjsuc2VBduFtqYyi" alt=""><figcaption></figcaption></figure>

The new task appears under the Tasks tab on the case page.


---

# 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/add-task.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.
