# Data Change Alerts

Canonical page: https://www.adaptabletools.com/docs/handbook-alerting-data-change

- The most common type of Alert in AdapTable is one which fires as the result of a Data Change rule
- This Data Change rule is a Boolean Expression - enabling multiple cell values to be included in the evaluation

Data Change Alerts are the most common type of Alert in AdapTable.

They fire whenever a change in the Grid's data matches the Rule in the Alert Definition.

This change can be either the result of a user's edit or of ticking data

## Rule

The Data Change Alert Rule is a (Boolean) Expression.

This is evaluated by [AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) - AdapTable's native Query Language.

- As the Rule is a full Expression, Alerts can fire based not just on the changed cell but other values in the same row
- And the Rule can combine multiple clauses with `AND` / `OR` logic

If the Expression evaluates to `true`, the Alert fires.

- An Expression with a [Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) of `All` (i.e. the whole row) is evaluated whenever any cell in the row is edited
- Specifying a limited [Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) of a few `ColumnIds` will restrict the edits that potentially trigger the Alert

**Example: Alerts: Boolean Expressions**

Alerts fired due to Boolean Expressions

- This example provides 3 Data Change Alerts, each firing on a different kind of Expression:
  - **Warning** (*GitHub Stars* Column) - a simple comparison: `[github_stars] < 1000`
  - **Success** (whole row) - multiple clauses combined with `AND`: `[github_stars] % 2 = 0 AND [language] = "TypeScript"`
  - **Info** (all *text* Columns) - a function using the in-scope Column: `IS_BLANK($SCOPE)`
- Each Alert displays a Notification and highlights the cell

- Edit *GitHub Stars* in any row to a value below 1000
- Edit a row where `language = TypeScript` so *GitHub Stars* becomes an even number
- Clear the value of any text cell (e.g. *Language* or *License*)

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Data Change Alert',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        // 1. Simple comparison on a single Column
        {
          Name: 'alert-github-stars',
          MessageType: 'Warning',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Rule: '[github_stars] < 1000',
          AlertProperties: {
            HighlightCell: true,
            DisplayNotification: true,
          },
        },
        // 2. Multiple clauses combined with AND - evaluated across the whole Row
        {
          Name: 'alert-success-typescript',
          MessageType: 'Success',
          Scope: {
            All: true,
          },
          Rule: '[github_stars] % 2 = 0 AND [language] = "TypeScript"',
          AlertProperties: {
            HighlightCell: true,
            DisplayNotification: true,
          },
        },
        // 3. Function using the in-scope Column ($SCOPE) across all text Columns
        {
          Name: 'alert-blank-text',
          MessageType: 'Info',
          Scope: {
            DataTypes: ['text'],
          },
          Rule: 'IS_BLANK($SCOPE)',
          AlertProperties: {
            HighlightCell: true,
            DisplayNotification: true,
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'week_issue_change',
            'github_watchers',
            'created_at',
            'updated_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

### Providing Data Change Alerts in Initial State

Each definition needs `Scope` (where edits are watched), `Rule` (boolean expression), and `MessageType`.

Use `ColumnIds`, `DataTypes`, or `{ All: true }` in [Column Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md).

The rule can reference the changed cell and other columns in the same row (e.g. comparisons, `AND` / `OR`, functions such as `IS_BLANK($SCOPE)`).

Common properties include `DisplayNotification`, `HighlightCell`, `JumpToCell`, and `LogToConsole` — see [Alert Behaviours](https://www.adaptabletools.com/docs/handbook-alerting-behaviours/index.md).

```ts [[1, 2, "Alert"], [1, 3, "AlertDefinitions"], [2, 8, "Scope"], [2, 9, "ColumnIds"], [2, 21, "Scope"], [2, 22, "All"], [3, 11, "Rule"], [3, 24, "Rule"], [4, 12, "AlertProperties"], [4, 13, "HighlightCell"], [4, 14, "DisplayNotification"], [4, 15, "JumpToCell"], [4, 25, "AlertProperties"], [4, 28, "LogToConsole"]]
const initialState: InitialState = {
  Alert: {
    AlertDefinitions: [
      {
        Name: 'alert-github-stars',
        MessageType: 'Warning',
        MessageHeader: 'Low GitHub Stars',
        Scope: {
          ColumnIds: ['github_stars'],
        },
        Rule: '[github_stars] < 1000',
        AlertProperties: {
          HighlightCell: true,
          DisplayNotification: true,
          JumpToCell: true,
        },
      },
      {
        Name: 'alert-typescript-even-stars',
        MessageType: 'Success',
        Scope: {
          All: true,
        },
        Rule: '[github_stars] % 2 = 0 AND [language] = "TypeScript"',
        AlertProperties: {
          HighlightCell: true,
          DisplayNotification: true,
          LogToConsole: true,
        },
      },
    ],
  },
};
```

The [AdapTableQL Guide](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) provides full details on writing Boolean Expressions
