> For the complete documentation index, see [llms.txt](https://docs.octoxlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.octoxlabs.com/documentation/adapters/how-adapters-work/data-rules.md).

# Data Rules

Assign a custom field value to every device that matches a condition, by creating data rules in Custom Adapter Settings or uploading them as a CSV file.

A data rule sets the value of a custom field on every device where another field has a given value. For example: "where `Domain` equals `corp.example.com`, set `Owner` to `IT Operations`". You define rules once, and OctoXLabs applies them to your devices after each discovery.

Data rules do not filter or exclude records, and they do not change the data that an adapter collects. They add your own fields next to it.

### How data rules work

A rule belongs to a **custom field** that you create in Custom Adapter Settings (under Settings > Data Source Settings). The custom field has a name, a type (Text, Number or Boolean) and an optional default value. Each rule on that field has three parts:

| Part                | What it is                                                                                                                                      |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **Search Field**    | The device field to look at. You pick it from the fields of a device adapter. It must be a field in OctoXLabs's device Field Map.               |
| **Search Value**    | The value to look for. Matching is an exact (equal) match.                                                                                      |
| **Value to assign** | The value written to the custom field on every matching device. It must fit the field type: a number for Number, `true` or `false` for Boolean. |

The rule list shows each rule as `Search Field = Search Value` and its value. A field, search field and search value combination can exist only once.

The assigned values appear on the device as custom data (the `custom-data` adapter in the device detail view), where you can search for them with a query like any other field. Rules work on the device inventory only.

#### When rules run

* **When you save a rule** or upload a file, OctoXLabs applies the new or changed rules to the devices of the latest discovery in the background. You do not need to wait for the next discovery.
* **After every discovery**, the post-discovery steps run **Sync Custom Data Rules to Devices**, which applies all rules to the new device data. This step runs only when **Enable Custom Adapter** is turned on under Settings > Advanced Settings (Advanced tab). It is off by default.

{% hint style="warning" %}
Turn on **Enable Custom Adapter** before you rely on data rules. Without it, rules apply when you save them but are not re-applied after the next discovery, so new devices do not get their values.
{% endhint %}

#### Rules that overlap

Avoid two rules that set the same custom field on the same device. Give each device one matching rule per field, so the result does not depend on the order in which rules are applied.

### Before you begin

* Applies to: OctoXLabs 6.3.0 and later. Version 6.3.0 changed data rules so that any field can be the search field; before that, custom values were matched by hostname only.
* Required role: a user with permission to view, add and change data rules, and to upload data rule files for the CSV method.
* The device field you want to match on exists in the Field Map. Run at least one discovery that returns the data you want to match.

### Create a data rule

{% stepper %}
{% step %}

#### Create the custom field

Open **Custom Adapter Settings**, select **Create Custom Data Field**, enter a **Name**, choose the **Type** and optionally a **Default**, then save. The name can contain only letters, hyphens and underscores (no digits or spaces).
{% endstep %}

{% step %}

#### Open the field and add a rule

Select the edit action of the field. Below the field form, the **Rule** table lists its rules. Select **Create Data Rule**.
{% endstep %}

{% step %}

#### Fill in the rule

Choose the **Adapter** (the default is `octoxlabs`) and a **Search Field** from that adapter's device fields. Enter the **Search Value** and the **Value to assign**, then select **Create**. A "Rule created" message confirms the rule.
{% endstep %}

{% step %}

#### Enable custom adapter for discovery runs

Open **Advanced Settings**, select the **Advanced** tab, tick **Enable Custom Adapter** and save. This lets every discovery re-apply the rules.
{% endstep %}
{% endstepper %}

To change or remove a rule, use the edit and delete actions in the rule table.

### Create many rules from a CSV file

To load a lot of rules at once, open **Custom Adapter Settings**, open the **Upload** menu and select **Upload Data Rule File**.

The file layout is one search field and one or more custom fields:

* The **first column header** is the search field, for example `Domain`. It must be a field in the Field Map.
* Every **other column header** is the name of a custom field. A field that does not exist yet is created, with its type detected from the values in the column (Boolean, Number or Text).
* In each row, the **first cell** is the search value and the other cells are the values to assign. An empty cell creates no rule.

```
Domain,Owner,Criticality
corp.example.com,IT Operations,3
branch.example.com,Branch IT,1
```

This file creates four rules: two for the `Owner` field and two for `Criticality`, matching on `Domain`.

{% hint style="info" %}
The on-screen note says the headers should start with `search_field`. Use the real name of the device field as the first header (for example `Domain`), because the upload checks that name against the Field Map.
{% endhint %}

Limits of the upload:

* The file must have a `.csv` extension, be UTF-8 encoded, and be up to 50 MB.
* It must have at least two columns. Custom field names follow the same rule as in the form: letters, hyphens and underscores.
* A search value or value longer than 255 characters is rejected for that row, and the other rows still load.
* A rule that already exists for the same field, search field and search value is skipped; the upload does not overwrite it. To change a value, edit the rule.
* The upload runs in the background. A progress bar shows the parsing and saving phases, and the first rows that failed are listed.

### Verify the result

Run a discovery, open the **Device Inventory** and search for a device that should match. In its detail view, the custom data section lists the custom field with the assigned value. You can also build a query on the custom field. See OctoXLabs Query Reference.

### Troubleshooting

| Symptom or error message                                            | Cause                                                                                                                             | Fix                                                                                      |
| ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `Search field must be in Field Map.`                                | The search field is not in the device Field Map.                                                                                  | Choose a field from the adapter's list, or run a discovery that returns the field first. |
| `Field, Search Field and Search Value must be unique`               | A rule with the same combination already exists.                                                                                  | Edit the existing rule instead.                                                          |
| `You must upload CSV file` or `CSV file must be UTF-8 encoded.`     | The file is not `.csv` or not UTF-8.                                                                                              | Save the file as CSV (UTF-8).                                                            |
| `CSV must have at least 2 columns.`                                 | The file has only the search field column.                                                                                        | Add at least one custom field column.                                                    |
| `Invalid field name: <name>` or `Invalid search field name: <name>` | A header contains characters other than letters, hyphens and underscores (the search field can also use dots, such as `OS.Type`). | Rename the header.                                                                       |
| `Search field '<name>' not found in Field Map.`                     | The first header is not a device field name.                                                                                      | Use the exact field name, which is case sensitive.                                       |
| Values are not updated after a new discovery                        | **Enable Custom Adapter** is off.                                                                                                 | Turn it on under Advanced Settings.                                                      |
| A rule exists but a device has no value                             | The search value does not match exactly (case, spaces), or the device was not in the latest discovery.                            | Check the stored value of the search field on the device and edit the rule.              |

### What's next?

<table data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>File-based adapters</strong></td><td>Import assets from CSV, XLSX, JSON and XML files.</td><td></td></tr><tr><td><strong>How adapters work</strong></td><td>See where data rules fit in the adapter and discovery flow.</td><td></td></tr><tr><td><strong>Discovery and Aggregation</strong></td><td>Learn when the post-discovery steps run.</td><td></td></tr><tr><td><strong>OctoXLabs Query Reference</strong></td><td>Query on the custom fields your rules assign.</td><td></td></tr></tbody></table>
