> For the complete documentation index, see [llms.txt](https://coldbox-orm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox-orm.ortusbooks.com/v5.x/advanced/coldbox-orm-event-handler/event-handler-cfc.md).

# Custom Event Handler

Extend the cborm event handler to add your own global ORM event logic

![](https://raw.githubusercontent.com/wiki/coldbox-modules/cbox-cborm/ORMEventHandler.jpg)

bx-orm calls one global event handler, the class named in `this.ormSettings.eventHandler`. To add your own global logic while keeping the ColdBox interception points, create a class that extends `cborm.models.EventHandler` and point the setting at it:

{% code title="models/MyEventHandler.bx" %}

```javascript
class extends="cborm.models.EventHandler" {

    public void function preInsert( any entity ){
        // your logic
        arguments.entity.setCreatedBy( getAuthUser() );
        // keep the ColdBox interception points working
        super.preInsert( argumentCollection = arguments );
    }

}
```

{% endcode %}

{% code title="Application.bx" %}

```javascript
this.ormSettings = {
    eventHandling : true,
    eventHandler  : "models.MyEventHandler"
};
```

{% endcode %}

Always call the parent method when you override one, or the matching ColdBox interception point is not announced. Below are the methods you can override:

| Listener Method                            | Description                                                                                                                                                                               |
| ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `postNew( entity, entityName )`            | Called after a new entity is created by `entityNew()`, `entityLoadOrNew()`, `entityLoadOrSave()` or a cborm service's `new()`. The entity is autowired here (announced once per `new()`). |
| `preLoad( entity )`                        | Called before the data is loaded from the database.                                                                                                                                       |
| `postLoad( entity )`                       | Called after the load operation is complete. The entity is autowired here.                                                                                                                |
| `preInsert( entity )`                      | Called just before the entity is inserted.                                                                                                                                                |
| `postInsert( entity )`                     | Called after the insert operation is complete.                                                                                                                                            |
| `preUpdate( entity, oldData )`             | Called just before the entity is updated. `oldData` is a struct of the original state of the entity.                                                                                      |
| `postUpdate( entity )`                     | Called after the update operation is complete.                                                                                                                                            |
| `preDelete( entity )`                      | Called before the entity is deleted.                                                                                                                                                      |
| `postDelete( entity )`                     | Called after the delete operation is complete.                                                                                                                                            |
| `preSave( entity )`                        | Called by the cborm services before a save.                                                                                                                                               |
| `postSave( entity )`                       | Called by the cborm services after a save.                                                                                                                                                |
| `postCommit( entity, entityName, action )` | **New in cborm 6**: called once an insert, update or delete is committed.                                                                                                                 |
| `onFlush( event )`                         | Called by bx-orm when a session flush starts. Announces `ORMPreFlush`.                                                                                                                    |
| `onAutoFlush( event )`                     | Called by bx-orm before a query, when Hibernate checks whether pending changes must be flushed first. Announces `ORMPreFlush`.                                                            |
| `preFlush( entities )`                     | Announces `ORMPreFlush` by hand, for code that did so in cborm 5.                                                                                                                         |
| `postFlush( entities )`                    | Announces `ORMPostFlush` by hand: bx-orm has no after-flush event.                                                                                                                        |


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox-orm.ortusbooks.com/v5.x/advanced/coldbox-orm-event-handler/event-handler-cfc.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
