> For the complete documentation index, see [llms.txt](https://docs-embed.anyflow.jp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-embed.anyflow.jp/solution-editor/actions/data-transform/variable.md).

# 変数アクション

## **概要**

Anyflow Embedでは、変数アクションを使用して変数を自由に定義することが可能です。\
変数アクションを使用することにより、型を変換したデータを様々な箇所で使用可能です。\
複雑なソリューションの中で、同様の変数を参照したい場合に有効的に活用します。

{% hint style="success" %}

### 変数の適用範囲について

作成した変数アクションのソリューション内でのみ有効です。\
異なるソリューション間で横断的に同様の変数を適用することはできません。\
異なるソリューション間で横断的にデータを利用したい場合は、[キーバリューストア](/solution-editor/actions/data-store/kvs.md) を利用します。
{% endhint %}

***

## **変数を作成**

変数を選択し、「変数を作成」アクションを選択します。

変数定義を設定します。\
指定されたデータ型の変数を作成可能です。\
各変数は使用するデータ型に合わせて設定する必要があります。（例:文字列、リスト型）

|         |         |
| ------- | ------- |
| 入力フィールド | 説明      |
| 名前      | 変数の名前   |
| 型       | 変数のデータ型 |

<figure><img src="https://files.readme.io/4975926-1.gif" alt="1432"><figcaption><p>変数を定義します</p></figcaption></figure>

オブジェクトの設定をします。

定義したい変数を入力します。\
変数定義で指定したデータ型によって、設定するオブジェクトの内容が異なります。\
テキストやエンドユーザー変数、Formulaでの設定も可能です。

<figure><img src="https://files.readme.io/778e198-2.png" alt="573"><figcaption><p>定義したい変数をオブジェクトに入力します</p></figcaption></figure>

***

変数を選択し、「Formulaを使って変数を作成」アクションを選択します。

[変数を作成](/solution-editor/actions/data-transform/variable.md)アクションと同様に、変数定義を設定をします。

Formulaの詳細の設定方法については、[Formulaページ](/solution-editor/common-specs/formula.md) で説明しています。

***

## **変数を更新**

変数を更新アクションでは、ソリューション内の変数を更新することが可能です。

### **変数を更新アクションの例**

以下の例では、

* ステップ2で、number という変数を Integer 型で定義し、値を 0 に設定しています。
* ステップ3で、10個の要素をもったリストを作成します。
* ステップ4で、繰り返しを10回に設定しています。
* ステップ5で、ステップ2で作成した変数に「+1をする」ように更新しています。

<figure><img src="https://files.readme.io/af0dcb6-Screen_Shot_2023-03-16_at_9.55.30_PM.png" alt=""><figcaption></figcaption></figure>

結果的に、繰り返しが終わったステップ6では、「10」が出力されます。

<figure><img src="https://files.readme.io/5799f9e-Screen_Shot_2023-03-16_at_9.58.12_PM.png" alt=""><figcaption></figcaption></figure>

同様に、配列の要素が文字列の場合には以下の様に設定することで要素を追加できます。

<figure><img src="https://files.readme.io/f167b42-image.png" alt="" width="563"><figcaption></figcaption></figure>

まず、以下のような変数を定義します。

```json
[{
  "name": "foo",
  "id": "bar"
}]
```

<figure><img src="https://files.readme.io/2fe7bbf-Screen_Shot_2023-03-16_at_10.02.49_PM.png" alt="667"><figcaption><p>itemsというリスト変数を、変数を作成アクションで作成</p></figcaption></figure>

変数を更新アクションを使ってリストに要素を追加するには、以下のような設定を行います。\
更新する対象の変数を選択し、変数をFormulaモードに切り替え、以下のようなFormulaを設定します。

この例では、items変数自身に、以下の要素を追加しています。

```json
[{
  "name": "item-" + str(インデックス番号),
  "id": インデックス番号
}]
```

<figure><img src="https://files.readme.io/c19f4cf-Screen_Shot_2023-03-16_at_10.09.09_PM.png" alt="1668"><figcaption><p>formulaへの入力時は1行で記述します</p></figcaption></figure>

結果、このように出力されます。

<figure><img src="https://files.readme.io/bb801f2-Screen_Shot_2023-03-16_at_10.11.01_PM.png" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs-embed.anyflow.jp/solution-editor/actions/data-transform/variable.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
