> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mailbox event, execute before message is saved

> CRMScript event model - Mailbox event, execute before message is saved

This is a CRMScript you can add under the menu **System design** in the screen **System Script**.

Here you can add CRMScript code that will be run for all mailboxes before the mail is taken by any mail filter, and also before it is saved.

There are several variables available in this context.

* To get a variable: `getVariable("xxx")`
* To set a variable: `setVariable("xxx", <value>)`, where \<value> is a valid value for the field you are trying to set.

Here follows a list of all available variables that are possible to modify.

| Variable       | Description                                              |
| -------------- | -------------------------------------------------------- |
| subject        | Subject of the mail                                      |
| author         | Author of the mail                                       |
| ticketExists   | 1 if this is a mail on an existing request, else 0       |
| sendAutoReply  | 1 if there will be sent an auto-reply, else 0            |
| whyNoAutoReply | Description on why there will not be sent any auto-reply |
| toTrashcan     | 1 if this mail will be sent to the trashcan, else 0      |
| toBeDeleted    | 1 if this mail will be deleted, else 0                   |

## Read-only variables

| Variable     | Description                                      |
| ------------ | ------------------------------------------------ |
| body         | The plain text part of the mail                  |
| bodyHtml     | The HTML part of the mail                        |
| ticketId     | The ticket ID, -1 if this is a new ticket        |
| fromEjournal | 1 if the mail is from another AIM Server, else 0 |
| isBounce     | 1 if this is a bounce, else 0                    |
| mailboxId    | The id of the mailbox that the mail came in to   |

<Note>
  If you load a ticket object with the given ticket ID, it is not recommended to change the values of this ticket. Later the ticket will be saved, and the values you have saved on the ticket might be overwritten. Thereby you should only modify the variables listed above.
</Note>

## Example

This script will generate a new request if the incoming email is originally supposed to be connected to an existing request. The script will tell the email engine to generate a new request only if the original request is closed.

```crmscript theme={null}
Ticket ticket;
ticket.load(getVariable("ticketId").toInteger());

if (ticket.getValue("status") == "2")
  setVariable("ticketExists", "0");  // Generate new request
```


## Related topics

- [Mailbox event, execute after message is saved](/en/automation/event-model/mailbox-after-save.md)
- [Execute Script As Event](/en/api/reference/restful/rest/crmscript/execute-script-as-event.md)
- [Execute Event Handlers](/en/api/reference/restful/agent/customerservice_agent/execute-event-handlers.md)
- [Execute Script As Event By Unique Id](/en/api/reference/restful/rest/crmscript/execute-script-as-event-by-unique-id.md)
