> For the complete documentation index, see [llms.txt](https://clerkal.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clerkal.gitbook.io/docs/guides/clerk-ai/logic.md).

# Logic

{% hint style="danger" %}
**Logic is an experimental feature and may change in the future**&#x20;
{% endhint %}

Logic is a way to turn your templates into smart templates. Enriching your template with logic allows it to auto-complete other sections based on the input or selection of elements.

Logic can only be set on certain elements:

* [**Text Fields**](/docs/guides/template-elements/text-fields.md)
* [**Checkboxes**](/docs/guides/template-elements/checkboxes.md)
* [**Buttons**](/docs/guides/template-elements/buttons.md)
* [**Dropdown**](/docs/guides/template-elements/dropdowns.md)
* [**Multi-select dropdown**](/docs/guides/template-elements/multi-select-dropdowns.md)
* [**Tooth selectors**](/docs/guides/template-elements/tooth-selectors.md)

## How logic works

Logic are groups of [**conditions**](#conditions) and [**actions**](#actions) (logic groups), that when set will auto-complete different sections of your template. [Multiple conditions](#chaining-conditions) can be set in a logic group, as well as multiple actions.

Once a condition has been met, then action will occur, e.g.:

1. When Button A is set to X
2. Set Text Field B to be Y

#### Example

To add more context to how logic work, lets consider the following example. In your template, you have 2 elements:

* A Button that defines a patients smoking statues with the following options:
  * Non-smoker
  * Smoker
* A Checkbox that contains smoking cessation advice, in your Discussion section

**We will create the following logic:**

* When the button option 'Smoker' is selected, set the checkbox with smoking cessation advice to active

<div data-with-frame="true"><figure><img src="/files/8SHDG06SZJRWcrgkQ0sz" alt=""><figcaption></figcaption></figure></div>

This will result in when a patient has been noted as a smoker, the smoking cessation advice will automatically be added to the output notes.

<div data-with-frame="true"><figure><img src="/files/cDRzputjJjiEEmwDKtXN" alt="" width="336"><figcaption></figcaption></figure></div>

## Adding logic to a Template

1. To start adding Logic to your Template, you need to enable editing mode.
2. Once enabled, click on the Logic icon in the Editor tool bar to open the Logic Panel

<div data-with-frame="true"><figure><img src="/files/8yNljJYu6bDcNJf9Tej9" alt="" width="356"><figcaption></figcaption></figure></div>

3. Start adding in your logics conditions and actions
   1. [**See conditions and actions below**](#conditions-and-actions)

### Adding comments

You can add comments within your logic to give more context to what it does. This is useful for the readability of the logic, remembering why you included certain elements or a further explanation of the logic itself.

<div data-with-frame="true"><figure><img src="/files/4EguKSRJU7ie8qN49vFg" alt=""><figcaption></figcaption></figure></div>

### Re-ordering Logic

Logic is executed in the order it is displayed in. To re-order your logic groups, simply drag the **6 dot icon**.

<div data-with-frame="true"><figure><img src="/files/0Yq9kjfrzlbhba6exEfL" alt=""><figcaption></figcaption></figure></div>

### Other features

The logic panel can be expanded or collapsed to view more of your logic

<div data-with-frame="true"><figure><img src="/files/GxXVPVQNj6uf1n0mTyIM" alt=""><figcaption></figcaption></figure></div>

Templates that have Logic set, will have the Logic icon displayed in the top bar.

<div data-with-frame="true"><figure><img src="/files/4OgAbfj2vAv3UHyce8cZ" alt="" width="544"><figcaption></figcaption></figure></div>

You can click on it to open the Logic panel and view the Template logic.

## Conditions and Actions

### Conditions

Conditions set the bases for your logic. When the **Condition** for a logic group is met, the **Action** will be executed.

#### Condition types

<table><thead><tr><th width="144.199951171875">Condition type</th><th width="186.59991455078125">Elements used on</th><th>Description</th></tr></thead><tbody><tr><td>Is</td><td>All</td><td><ul><li>If the elements value matches this, the condition is met. </li><li>It can be left empty to define being blank, or un-selected</li></ul></td></tr><tr><td>Is not</td><td>All</td><td><ul><li>If the elements value IS NOT this value, the condition is met.</li><li>It can be left empty to define being blank or unselect.</li></ul></td></tr><tr><td>contains</td><td>All</td><td><ul><li>If the elements value CONTAINS this value, the condition is met.</li><li>This value cannot be left empty.</li></ul></td></tr><tr><td>Is populated and active</td><td>All</td><td><ul><li>If the element is populated and active (i.e on an active line), the condition is met.</li><li>If this condition is set, you can use the <strong>Set to condition value</strong> action, see here for more information.</li></ul></td></tr><tr><td>Has state</td><td><a href="/pages/461e6200c696c0fbcdc0364173a0b1b054eee27a"><strong>Checkboxes</strong></a></td><td><ul><li><p>To define the checked state of the checkbox for the condition to be met:</p><ul><li>E.g, checked or unchecked.</li></ul></li></ul></td></tr><tr><td>Has selected</td><td><a href="/pages/587c41627eafccc81965efbe387fddb250af323e"><strong>Buttons</strong></a>, <a href="/pages/556844527e386e96e2881897fce011bc559f8442"><strong>Multi-select Dropdowns</strong></a></td><td><ul><li>Allows you to specify what options in the element and their state to have the condition met.</li></ul></td></tr></tbody></table>

#### Using 'Has Selected'

When using the **'Has Selected'** condition, you can define all or certain options within the element to form the logic. This is useful for handling context specific log.

You can also set each selected options state to make the condition value, e.g. if the option is **selected** or **unselected**.

<div data-with-frame="true"><figure><img src="/files/sJFQnrxXNQCvea40H1M0" alt="" width="462"><figcaption></figcaption></figure></div>

Setting the **Combination** value can allow you to further refine your condition. This can be set to either **ANY** or **EXACT**:

* **ANY** - means that *ANY* of the options being selected will trigger meet the condition
* **EXACT** - means that the *EXACT* combination needs to be selected to meet the condition.

#### Tooth selector conditions

When setting the conditions for a Tooth Selector, you must choose the options in its menu that will meet the condition

#### Chaining Conditions

Conditions can be chained **to add multiple conditions** as part of your **logic group**. When chaining conditions, you can define ‘**AND**’ or ‘**OR**’

* **AND** - means that condition 1 AND condition 2 etc need to be met for the condition to be valid
* **OR** - means that at least 1 of the conditions defined need to be met for the condition to be active.

<div data-with-frame="true"><figure><img src="/files/gmtloq10L2V8et4dLzVg" alt=""><figcaption></figcaption></figure></div>

### Actions

Actions define what should actually happen when a logic condition has been met. Some action types are element specific.

You can also set multiple actions to occur as part of the logic group, similar to condition chaining.

#### Action Types

<table><thead><tr><th width="144.199951171875">Action type</th><th width="186.59991455078125">Elements used on</th><th>Description</th></tr></thead><tbody><tr><td>Set text</td><td>All</td><td><ul><li>Defines to set the Action elements text value to the defined value. </li><li>It can be left empty to define being blank, or un-selected</li></ul></td></tr><tr><td>Set to conditions value</td><td>All</td><td><ul><li>Defines to set the Action elements value to the same value of the condition element.</li><li>This can only be set if the condition type was <strong>'Is populated and active'</strong> </li><li>Cannot be used with chained conditions</li></ul></td></tr><tr><td>Set state</td><td><a href="/pages/461e6200c696c0fbcdc0364173a0b1b054eee27a"><strong>Checkboxes</strong></a></td><td><ul><li>Defines the checked state of an Action checkbox, e.g: checked or unchecked.</li></ul></td></tr><tr><td>Set selected</td><td><a href="/pages/587c41627eafccc81965efbe387fddb250af323e"><strong>Buttons</strong></a>, <a href="/pages/556844527e386e96e2881897fce011bc559f8442"><strong>Multi-select Dropdowns</strong></a></td><td><ul><li>Defines the Action elements options to set, as part of the action. </li></ul></td></tr></tbody></table>

#### Using 'Set Selected'

When using the **'Set Selected'** action, you can define which options in the element you want to be set.&#x20;

You can also set what each options state should be e.g. **selected** or **unselected**.

Setting the **Behaviour** value will allow you to override existing options set on the element or preserve already set options. This can be set to either **SET** or **INCLUDE**:

* **SET** - means to *SET* the elements options to the combination defined, removing any already set values
* **INCLUDE** - means it will add (*INCLUDE*) these defined combination, keeping any already set values unchanged.

#### Actions on Tooth Selectors

When setting an action on a Tooth Selector, you will be required to select the elements options to set in its menu.
