> 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/mapping-a-status-onto-other-fields.md).

# Mapping a Status Onto Other Fields

A status field is usually the summary of a decision that has other consequences. When a record reaches a new status there is often a set of flags to default, a date to stamp and somebody to tell. Putting all of that behind the status change means the person doing the work only has to record the decision once.

## The scenario

A school records the outcome of an admission. A pupil who becomes Enrolled needs the paperwork flags set up for collection, and whether they have a sibling already on roll needs recording as a plain Yes or No. A pupil who is Withdrawn needs the date stamped and admissions told.

| Setting         | Value                                                  |
| --------------- | ------------------------------------------------------ |
| Trigger field   | `5201` Admission status                                |
| Read            | `5202` Sibling on roll (reference to another case)     |
| Written by ID   | `5203` Date withdrawn                                  |
| Written by name | `MedicalFormReceived`, `PhotoConsent`, `SiblingOnRoll` |
| Template        | `PupilWithdrawn-Admissions`                            |

## The script

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

function after_save() {
    var status = Changed.CustomField("5201").NewValueAsText;

    if (status == "Enrolled") {
        Service.UpdateCustomField("MedicalFormReceived", "No");
        Service.UpdateCustomField("PhotoConsent", "No");

        if (Service.CustomField("5202").ValueAsText === "") {
            Service.UpdateCustomField("SiblingOnRoll", "No");
        } else {
            Service.UpdateCustomField("SiblingOnRoll", "Yes");
        }

        Service.AddMessage("Medical form and photo consent have been flagged for collection.", "notify-success");
        return true;
    }

    if (status == "Withdrawn") {
        var today = new Date();
        var todayStamp = today.toISOString().slice(0, 10).replace(/-/g, "");

        Service.UpdateCustomField("5203", todayStamp);
        Service.SendEmail("PupilWithdrawn-Admissions");
        return true;
    }

    return true;
}
```

## Fields by name and by ID

This script identifies some fields by numeric ID and others by name, and both are accepted by `UpdateCustomField`. Names are matched with spaces removed and capitalisation ignored, so `MedicalFormReceived`, `Medical Form Received` and `medical form received` all reach the same field.

Names read better, but they break if somebody relabels the field, and a write to a field that cannot be found is dropped silently rather than reported. IDs never change, so prefer them for anything you rely on, and keep names for fields whose labels are settled.

## Turning a reference into a flag

`5202` holds a reference to a sibling's case, which is useful on screen but awkward to report on or filter by. Testing whether it is empty and writing a plain Yes or No into `SiblingOnRoll` gives a field that behaves like any other Yes/No answer.

Note that this is derived, not entered. Anyone who links a sibling later will not re-trigger this script, since it watches the admission status rather than the reference. If the flag needs to stay accurate, trigger a second script from `5202` as well.

## Stamping today's date

The date is built from JavaScript's own `Date` and reduced to `yyyyMMdd`:

```javascript
var today = new Date();
var todayStamp = today.toISOString().slice(0, 10).replace(/-/g, "");
```

`toISOString` gives something like `2026-04-01T09:15:00.000Z`, the slice keeps the date part, and the replace removes the hyphens to leave `20260401`.

{% hint style="info" %}
`toISOString` always works in UTC. For a script that runs late in a British summer evening, that can name yesterday. Where the exact day matters, `Service.GetCurrentDate()` returns the server's own date as `yyyy-MM-dd`.
{% endhint %}

The date is only worked out inside the branch that needs it. It is a small thing, but it keeps the cost and the reasoning next to the one status that uses them.


---

# 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/mapping-a-status-onto-other-fields.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.
