---
schema_version: "1"
id: b314bc2249921910bfe27fe7c1ae88b4b746ee15735dd17fb8c7ff02ebc1ae88
title: カスタム追加関数の定義
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.7/dataweave-cookbook-define-a-custom-addition-function.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-define-a-custom-addition-function
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 30178565096889d7697f6058f20f7484eb8c61eb82d824e7364f90de965d26f7
description: MuleSoft Documentation Site
release: "2.7"
latest: false
---
# カスタム追加関数の定義

この DataWeave の例では、一連の項目の価格と在庫数を取得し、いくつかの関数を使用して小計を計算し、割引を差し引き、税金を加算します。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

以下を使用します。

- 入力のすべての項目を調べる ​`map`​ 関数。
- 基本的な算術演算 (​`*`​、​`+`​、​`-`​ など)。
- `++`​ を使用した文字列連結。
- 文字列値を数値に強制変換する ​`as`​。
- 税額と割引額を設定するヘッダーの固定値。
- 1 回定義してコードで複数回使用するヘッダーのカスタム関数。
- さまざまな項目を 1 つの合計に集約する ​`reduce`​ 関数。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
var tax = 0.085
var discount = 0.05
fun getSubtotal (items) = items reduce ((item, accumulator = 0) ->
        accumulator + (item.unit_price * item.quantity * (1 - discount)))
---
invoice: {
    header: payload.invoice.header,
    items: { (payload.invoice.items map {
        item : {
            description: $.description,
            quantity: $.quantity,
            unit_price: $.unit_price,
            discount: (discount * 100) as Number ++ "%",
            subtotal: $.unit_price * $.quantity * (1 - discount)
        }
    }) },
    totals:
    {
        subtotal: getSubtotal(payload.invoice.items ),
        tax: (tax * 100) as Number ++ "%",
        total: getSubtotal(payload.invoice.items ) * (1 + tax)
    }
}
```

入力 JSON ペイロード:

```json
{
  "invoice": {
    "header": {
      "customer_name": "ACME, Inc.",
      "customer_state": "CA"
    },
    "items": [
      {
        "description": "Product 1",
        "quantity": "2",
        "unit_price": "10"
      },
      {
        "description": "Product 2",
        "quantity": "1",
        "unit_price": "30"
      }
    ]
  }
}
```

出力

```json
{
  "invoice": {
    "header": {
      "customer_name": "ACME, Inc.",
      "customer_state": "CA"
    },
    "items": {
      "item": {
        "description": "Product 1",
        "quantity": "2",
        "unit_price": "10",
        "discount": "5%",
        "subtotal": 19.00
      },
      "item": {
        "description": "Product 2",
        "quantity": "1",
        "unit_price": "30",
        "discount": "5%",
        "subtotal": 28.50
      }
    },
    "totals": {
      "subtotal": 47.50,
      "tax": "8.500%",
      "total": 51.53750
    }
  }
}
```

## [](#関連する例)関連する例

- [データのマップ](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-map)
- [定数ディレクティブの使用](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-use-constant-directives)
- [関数を使用したリストの条件付き削減](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-conditional-list-reduction-via-function)
- [リスト内のデータをフラット化する関数の定義](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-define-function-to-flatten-list)
- [関数を引数として渡す](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-pass-functions-as-arguments)

## [](#関連情報)関連情報

- [DataWeave Core 関数](https://docs.mulesoft.com/jp/dataweave/2.7/dw-core)
- [DataWeave セレクター](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-selectors)
- [DataWeave の解説書](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook)