# Custom hook

## Pre run hook

At the start of each sync run, the Sync Engine lists all stored procedures named `SYNC_RUN_PRE_HOOK`, with a single `object` parameter. All that exist are executed with the following parameter value:

```
{
  "branch_id": <the id of the branch, or null>,
  "new_health_state": <the new health state of the run, e.g. "HEALTHY">,
  "sync_id": <the id of the sync>,
  "sync_slug": <the slug of the sync>,
  "sync_run_id": <the id of the sync run>,
  "previous_sync_run_id", <the id of the previous sync run, or null if this is the first run>,
  "previous_health_state", <the health state of the previous run, or null if this is the first run>
}
```

Note: Health states are listed [here](https://docs.omnata.com/omnata-product-documentation/omnata-sync-for-snowflake/how-it-works/terminology#sync-health-states).

## Post run hook

At the end of each sync run, the Sync Engine lists all stored procedures named `SYNC_RUN_POST_HOOK`, with a single `object` parameter. All that exist are executed with the following parameter value:

```
{
  "branch_id": <the id of the branch, or null>,
  "new_health_state": <the new health state of the run, e.g. "HEALTHY">,
  "sync_id": <the id of the sync>,
  "sync_slug": <the slug of the sync>,
  "sync_run_id": <the id of the sync run>
}
```

Note: Health states are listed [here](https://docs.omnata.com/omnata-product-documentation/omnata-sync-for-snowflake/how-it-works/terminology#sync-health-states).

## Example

Below is an example of a hook which simply writes the run information to a table:

```
create table TEST_HOOK_LOG(payload OBJECT);

CREATE OR REPLACE PROCEDURE SYNC_RUN_POST_HOOK(result OBJECT)
RETURNS VARCHAR NOT NULL
LANGUAGE SQL
EXECUTE AS OWNER
AS
BEGIN
  INSERT INTO TEST_HOOK_LOG select :result;
  RETURN 'Rows inserted: ' || SQLROWCOUNT;
END;

grant usage on procedure SYNC_RUN_POST_HOOK(OBJECT) to application OMNATA_SYNC_ENGINE;

-- you also need to grant usage on SYNC_RUN_POST_HOOK's database and schema
grant usage on database <DB> to application OMNATA_SYNC_ENGINE;
grant usage on schema <DB>.<Schema> to application OMNATA_SYNC_ENGINE;

```

{% hint style="warning" %}
The procedure must be created with Owners rights, or an error will be raised.

The Sync Engine will wait for the procedure to complete before the run finishes, so it may be better to start a task for log running processes.
{% endhint %}


---

# Agent Instructions: 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:

```
GET https://docs.omnata.com/omnata-product-documentation/omnata-sync-for-snowflake/integrations/custom-hook.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
