はじめに
Power Apps
とPower Automate
の連携、
非常に強力な機能です。
豊富なコネクタから、テクニカルな自動化まで一気通貫で担うことができます。
しかし、Power AppsからPower Automateフローをトリガーするに当たり、
引数の設定が若干手間だったりします。
FlowtriggeredbyaPowerapp.Run(
EditForm3.LastSubmit.Email,
TextInput1.Text
);
引用元 Microsoft learn
引数をPower Automate側で、あらかじめ設定しておく必要があります。
トリガー PowerApps (V2)を覗いてみる
Power AppsからPower Automateに、データを渡すときは、PowerApps (V2) トリガーから、変数の型を指定して取得します。
{
"type": "Request",
"kind": "PowerAppV2",
"inputs": {
"schema": {
"type": "object",
"properties": {
"text": {
"description": "入力を指定してください",
"title": "入力",
"type": "string",
"x-ms-content-hint": "TEXT",
"x-ms-dynamically-added": true
},
"boolean": {
"description": "[はい] または [いいえ] を選択してください",
"title": "はい/いいえ",
"type": "boolean",
"x-ms-content-hint": "BOOLEAN",
"x-ms-dynamically-added": true
},
"file": {
"description": "ファイルまたはイメージを選択してください",
"title": "ファイルのコンテンツ",
"type": "object",
"properties": {
"contentBytes": {
"type": "string",
"format": "byte"
},
"name": {
"type": "string"
}
},
"x-ms-content-hint": "FILE",
"x-ms-dynamically-added": true
},
"email": {
"description": "メール アドレスを入力してください。",
"format": "email",
"title": "電子メール",
"type": "string",
"x-ms-content-hint": "EMAIL",
"x-ms-dynamically-added": true
},
"number": {
"description": "数を入力してください",
"title": "数",
"type": "number",
"x-ms-content-hint": "NUMBER",
"x-ms-dynamically-added": true
},
"date": {
"description": "日付を入力または選択してください (YYYY-MM-DD)",
"format": "date",
"title": "トリガーの日付",
"type": "string",
"x-ms-content-hint": "DATE",
"x-ms-dynamically-added": true
}
},
"required": [
"text",
"boolean",
"file",
"email",
"number",
"date"
]
}
}
}
Power Automateの変数の型に合わせて、Power Appsから値を受け取ることができます。
日本語表記 | データ型 |
---|---|
テキスト | text |
はい/いいえ | boolean |
ファイル | file |
電子メール | |
数値 | number |
日付 | date |
値を呼び出す方法
triggerBody関数で値を受け取ります。
若干曲者ですが、triggerBody関数の引数は、PowerApps (V2)
の中で暗黙的に決まります。
「引数はこれで正しいのかな🧐」と怖くなる時もあります。
パラメーターで名前を工夫しても・・・
引数は
- text
- number
- text_1
と自動的に決まってしまいます。
{
**引数!** "text": {
"description": "入力を指定してください",
"title": "入力",
"type": "string",
"x-ms-content-hint": "TEXT",
"x-ms-dynamically-added": true
},
**引数!** "number": {
"description": "数を入力してください",
"title": "数",
"type": "number",
"x-ms-content-hint": "NUMBER",
"x-ms-dynamically-added": true
},
**引数!** "text_1": {
"description": "入力を指定してください",
"title": "入力 1",
"type": "string",
"x-ms-content-hint": "TEXT",
"x-ms-dynamically-added": true
},
**引数!** "text_2": {
"description": "入力を指定してください",
"title": "Text",
"type": "string",
"x-ms-content-hint": "TEXT",
"x-ms-dynamically-added": true
},
**引数!** "text_3": {
"description": "入力を指定してください",
"title": "Str",
"type": "string",
"x-ms-content-hint": "TEXT",
"x-ms-dynamically-added": true
},
**引数!** "number_1": {
"description": "数を入力してください",
"title": "Integer",
"type": "number",
"x-ms-content-hint": "NUMBER",
"x-ms-dynamically-added": true
},
**引数!** "boolean": {
"description": "[はい] または [いいえ] を選択してください",
"title": "はい/いいえ",
"type": "boolean",
"x-ms-content-hint": "BOOLEAN",
"x-ms-dynamically-added": true
},
**引数!** "boolean_1": {
"description": "[はい] または [いいえ] を選択してください",
"title": "はい/いいえ 1",
"type": "boolean",
"x-ms-content-hint": "BOOLEAN",
"x-ms-dynamically-added": true
}
}
画面で多くの引数を設定するのも、なんとなく手間を感じます。
JSON の解析 を使う - シンプルな文字列
こんなときに私は、Power Automateのデータ操作
におけるJSON の解析
を使用します。
JSON の解析
を使うことによって、
ひとつの文字列から、オブジェクトとして複数の値を受け取ることができるからです。
試しに、Power Apps
からPower Automate
にオブジェクト文字列を送信します。
UpdateContext(
{
Obj : {
ObjectText: "Text",
ObjectString: "Power Apps",
Number: 365,
Boolean: true,
Date: Today()
}
}
);
ParseJSON.Run(JSON(Obj));
Power Automate側は、文字列
としてJSON関数の結果を受け取ります。
受け取る値はコチラ。
{"Boolean":true,"Date":"2024-05-09","Number":365,"ObjectString":"Power Apps","ObjectText":"Text"}
こちらを、JSON の解析
からサンプルのペイロードを使用してスキーマを生成する
と
{
"type": "object",
"properties": {
"Boolean": {
"type": "boolean"
},
"Date": {
"type": "string"
},
"Number": {
"type": "integer"
},
"ObjectString": {
"type": "string"
},
"ObjectText": {
"type": "string"
}
}
}
サンプルに合わせて、データ型を定義し、
{
"Boolean": true,
"Date": "2024-05-09",
"Number": 365,
"ObjectString": "Power Apps",
"ObjectText": "Text"
}
後続の処理に使うことができます。
文字列から値を評価するからこそ、なせるテクニックです。
Power Appsの式エディタのほうが、コードエディタに近しい形で、変数を定義できるため、
Power Appsからまとめて値を送ってしまいたいな、というときに便利です。
配列(Array)を渡すこともできる
JSON関数で、JSONFormat.FlattenValueTables
を設定し、値配列を渡すと、
ParseJSON.Run(JSON(["a", "b", "cde"],JSONFormat.FlattenValueTables));
Power Appsで、扱いづらかった配列が渡せます。
配列にオブジェクトを格納して渡す
私の記事の中で、かなり多く反響をいただいた記事にも使用しましたが
、Table関数をJSON
に変換して渡すことで、
配列に格納されたオブジェクトとして評価され、動的な数の配列として使用することもできます。
ParseJSON.Run(JSON(Table({Item:"Violin123",Location:"France",Owner:"Fabrikam"})))
オブジェクト変数の中に、Table関数、値配列の場合
- オブジェクトの中にTable関数
オブジェクトの中に、Table関数や値配列
を含めて、解析を試みた場合、
オブジェクトの中の配列として、活用ができます。
UpdateContext(
{
Obj : {
ObjectText: "Text",
TableArray: Table({Item:"Violin123",Location:"France",Owner:"Fabrikam"},{Item:"Violin456",Location:"Chile"}),
Number: 365,
Boolean: true,
Date: Today()
}
}
);
ParseJSON.Run(JSON(Obj));
解析を通じて、データ型が狙い通りに評価されました。
{
"type": "object",
"properties": {
"Boolean": {
"type": "boolean"
},
"Date": {
"type": "string"
},
"Number": {
"type": "integer"
},
"ObjectText": {
"type": "string"
},
"TableArray": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Item": {
"type": "string"
},
"Location": {
"type": "string"
},
"Owner": {
"type": "string"
}
},
"required": [
"Item",
"Location"
]
}
}
}
}
- オブジェクトの中に値配列
値配列[]
を内包してみると、どうなるでしょうか。
UpdateContext(
{
Obj : {
ObjectText: "Text",
TableArray: [1,2,3],
Number: 365,
Boolean: true,
Date: Today()
}
}
);
ParseJSON.Run(JSON(Obj));
上記の場合、配列がkey:value
の形式になります。
{
"type": "object",
"properties": {
"Boolean": {
"type": "boolean"
},
"Date": {
"type": "string"
},
"Number": {
"type": "integer"
},
"ObjectText": {
"type": "string"
},
"TableArray": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Value": {
"type": "integer"
}
},
"required": [
"Value"
]
}
}
}
}
JSON([1, 2, 3],JSONFormat.FlattenValueTables)
を使ってみると、
UpdateContext(
{
Obj : {
ObjectText: "Text",
TableArray: JSON([1, 2, 3],JSONFormat.FlattenValueTables),
Number: 365,
Boolean: true,
Date: Today()
}
}
);
ParseJSON.Run(JSON(Obj));
TableArray
が文字列(string)
として評価されてしまいました・・・。
{
"type": "object",
"properties": {
"Boolean": {
"type": "boolean"
},
"Date": {
"type": "string"
},
"Number": {
"type": "integer"
},
"ObjectText": {
"type": "string"
},
"TableArray": {
"type": "string"
}
}
}
なかなか奥が深い。
おわりに
なんとなくやりづらかった操作も活路が見えますが、
このJSONの解析
を使うことによって、発生するエラーもあります。
活用される場合は、エラーと向き合う覚悟をもちつつ、時と場合を考えて使ってみましょう!
記事のいいね❤
や拡散は、著者が非常に喜ぶのでジャンジャンしてくださいね♪