> Markdown versions of all pages of this connector library are available by appending `.md` to the page URL. For the full index of connectors, see [llms.txt](https://connectors.appmixer.com/llms.txt).

# appmixer.utils.controls.CodeBlock

- Component: `appmixer.utils.controls.CodeBlock`
- Kind: Action
- Version: 1.0.0
- Visibility: Public
- Connector: [appmixer.utils.controls 1.14.7](https://connectors.appmixer.com/connector/appmixer.utils.controls/1.14.7.md)
- Component page: <https://connectors.appmixer.com/connector/appmixer.utils.controls/1.14.7/component/appmixer.utils.controls.CodeBlock>

Allows you to write JavaScript code to transform data. Executes the provided code when the component receives input.

## Input ports

### Port `in`

| Field | Type | Description |
| --- | --- | --- |
| Code (`code`) | textarea | Write JavaScript code to transform data. Access input data via $data object (e.g., $data.value). - No top-level return statements - the last expression is automatically returned. - To use return, wrap in IIFE: `(function() { return $data.value * 2; })()` - To return JSON object, use JSON.stringify: `JSON.stringify({ name: $data.name, isAdult: $data.age >= 18, email: $data.email.toLowerCase() });` - Define your variables in the 'Variables' form below. Variables will be accessible in the code as `$data.<variable name>`. |
| Variables (`variables`) | expression | Provide the variables. |

## Output ports

### Port `out`

| Field | Type | Description |
| --- | --- | --- |
| Result (`result`) | object | - |

## Full metadata (component.json)

```json
{
  "name": "appmixer.utils.controls.CodeBlock",
  "author": "Appmixer <info@appmixer.ai>",
  "description": "Allows you to write JavaScript code to transform data. Executes the provided code when the component receives input.",
  "version": "1.0.0",
  "inPorts": [
    {
      "name": "in",
      "schema": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "variables": {
            "type": "object"
          }
        },
        "required": [
          "code"
        ]
      },
      "inspector": {
        "inputs": {
          "code": {
            "type": "textarea",
            "label": "Code",
            "index": 1,
            "tooltip": "Write JavaScript code to transform data. Access input data via $data object (e.g., $data.value).<br/><br/>- No top-level return statements - the last expression is automatically returned.<br/>- To use return, wrap in IIFE:<br/> <code>(function() { return $data.value * 2; })()</code><br/>- To return JSON object, use JSON.stringify:<br/><code>JSON.stringify({<br/>&nbsp;&nbsp;name: $data.name,<br/>&nbsp;&nbsp;isAdult: $data.age &gt;= 18,<br/>&nbsp;&nbsp;email: $data.email.toLowerCase()<br/>});</code><br/>- Define your variables in the 'Variables' form below. Variables will be accessible in the code as <code>$data.&lt;variable name&gt;</code>."
          },
          "variables": {
            "type": "expression",
            "label": "Variables",
            "levels": [
              "ADD"
            ],
            "index": 2,
            "tooltip": "Provide the variables.",
            "fields": {
              "text": {
                "type": "textarea",
                "label": "Value",
                "index": 1,
                "tooltip": "Enter a text value.",
                "when": {
                  "eq": {
                    "./type": "text"
                  }
                }
              },
              "date-time": {
                "type": "date-time",
                "label": "Value",
                "index": 1,
                "tooltip": "Pick a date and time.",
                "when": {
                  "eq": {
                    "./type": "date-time"
                  }
                }
              },
              "number": {
                "type": "number",
                "label": "Value",
                "index": 1,
                "tooltip": "Enter a number value.",
                "when": {
                  "eq": {
                    "./type": "number"
                  }
                }
              },
              "toggle": {
                "type": "toggle",
                "label": "Value",
                "index": 1,
                "defaultValue": false,
                "tooltip": "Toggle a value.",
                "when": {
                  "eq": {
                    "./type": "toggle"
                  }
                }
              },
              "name": {
                "type": "text",
                "label": "Name",
                "index": 2,
                "tooltip": "Enter the variable name."
              },
              "type": {
                "type": "select",
                "label": "Type",
                "index": 3,
                "tooltip": "Select the variable type.",
                "defaultValue": "text",
                "options": [
                  {
                    "label": "Number",
                    "value": "number"
                  },
                  {
                    "label": "Text",
                    "value": "text"
                  },
                  {
                    "label": "Date",
                    "value": "date-time"
                  },
                  {
                    "label": "Boolean",
                    "value": "toggle"
                  }
                ]
              }
            }
          }
        }
      }
    }
  ],
  "outPorts": [
    {
      "name": "out",
      "options": [
        {
          "label": "Result",
          "value": "result",
          "schema": {
            "type": "object",
            "description": "Result of the executed code. Structure depends on the code output."
          }
        }
      ]
    }
  ],
  "icon": "(data URI omitted)"
}
```
