スクリプトの実行
呼び出し元スクリプトの操作有効範囲中にスクリプトを同期的に実行します。
コマンドの可用性: IBM RPA SaaS および IBM RPA オンプレミス
説明
「スクリプトの実行」 (executeScript) コマンドは、 「スクリプトの実行」と呼ばれるスクリプトの操作中に、スクリプトをロードし、そのコマンドを同期的に実行します。 参照用として、 「スクリプトの実行」 コマンドを使用するスクリプトは呼び出し元スクリプトと呼ばれ、「スクリプトの実行」コマンドで参照されるスクリプトは参照先スクリプトと呼ばれます。
「スクリプトの実行」 を使用して、 IBM RPA サーバーのテナントからローカル・スクリプトまたはスクリプトをロードできます。 Bot Runtime は、以下の必要条件の少なくとも 1 つを満たす場合に、参照されたスクリプトを後で使用するためにキャッシュに入れます。
- ローカル・スクリプトを参照しています。
- テナントから特定のバージョンのスクリプトを参照しています。
上記の必要条件のいずれも満たさない場合、Bot Runtime は、 「スクリプトの実行」を呼び出すたびに、参照されているスクリプトをダウンロードします。
参照されるスクリプトに入力パラメーターがある場合、これらのパラメーターに初期値を送信できます。つまり、これらのパラメーターまたは変数は、参照されるスクリプトの開始時にデフォルト値ではなく、送信した値を保持します。
参照されるスクリプトに出力パラメーターがある場合、参照されるスクリプトが終了すると、これらのパラメーターを呼び出し元スクリプトの変数にバインドすることによって、その値を呼び出し元スクリプトにロードすることができます。
参照されたスクリプトは、呼び出し元スクリプトの操作中に同期的に実行されます。 つまり、 「スクリプトの実行」 を実行すると、参照されたスクリプトのコマンドが 1 つずつ実行され、呼び出し元スクリプトはこの操作の終了を待機します。 参照されたスクリプトが失敗した場合は、 「エラーの処理」 (handleError) パラメーターを有効にして「スクリプトの実行」コマンドのエラーを無視するか、 「エラーの処理」 (onError) コマンドを使用してエラー・イベントをキャプチャーすることができます。
「エラーの処理 (Handle error)」 パラメーターを有効にした場合、 「スクリプトの実行 (Execute Script)」 を呼び出したスクリプトが失敗すると、 「スクリプトの実行 (Execute Script)」 を実行するスクリプトの error 環境変数に、失敗したスクリプトからのエラー・メタデータが含まれます。 error 環境変数が保管するメタデータについて詳しくは、 error 環境変数 を参照してください。
スクリプト構文
IBM RPA の専有スクリプト言語の構文は、他のプログラミング言語と似ています。 スクリプト構文は、スクリプト・ファイル内のコマンドの構文を定義します。 この構文は、 IBM RPA Studioの 「スクリプト」 モードで操作できます。
executeScript [--handleError(Boolean)] [--isfromfile(Boolean)] --filename(String) --name(String) [--parameters(String)] [--output(String)] [--version(Numeric)] (Boolean)=value (String)=errormessage (Numeric)=linenumber (String)=errorsubname (Error)=error
入力パラメーター
以下の表は、このコマンドで使用可能な入力パラメーターのリストを示しています。 この表では、 IBM RPA Studioのスクリプト・モードで作業しているときのパラメーター名と、それに相当する Designer モードのラベルを確認できます。
| 「デザイナー」モードのラベル | 「スクリプト」モードの名前 | 必須 | 使用可能な変数タイプ | 説明 |
|---|---|---|---|---|
| エラーを処理 | handleError |
Optional |
Boolean |
「エラーの処理 (Handle error)」 パラメーターを有効にした場合、 「スクリプトの実行 (Execute Script)」 を呼び出したスクリプトが失敗すると、 「スクリプトの実行 (Execute Script)」 を実行するスクリプトの error 環境変数に、失敗したスクリプトからのエラー・メタデータが含まれます。 error 環境変数が保管するメタデータについて詳しくは、 error 環境変数 を参照してください。 |
| ファイル | isfromfile |
Optional |
Boolean |
有効にする場合は、 Filename パラメーターに絶対ファイル・パスを指定して、ローカル・スクリプトを参照する必要があります。 無効にした場合は、 「名前」 パラメーターで IBM RPA Control Center テナントからスクリプトを参照する必要があります。 このパラメーターは 非推奨です。 代わりに Name パラメーターを使用してください。 |
| ファイル名 | filename |
Required when the File parameter is True |
Text |
実行するスクリプトの絶対ファイル・パス。 このパラメーターは、 非推奨です。 このパラメーターからのデータは、 Name 入力パラメーターに自動的にマイグレーションされます。 |
| 名前 | name |
Required when the File parameter is False |
Text |
実行するテナントからのスクリプトの名前。 スクリプトへの絶対ファイル・パス、または実行するスクリプトの名前のいずれかを受け入れます。 名前のみを指定する場合は、ログインしている同じテナントにスクリプトが既に公開されている必要があります。 |
| パラメーター | parameters |
Optional |
Text |
参照されるスクリプトからの入力パラメーターを呼び出し元スクリプトの値または変数にバインドし、参照されるスクリプトが操作を開始するときに、呼び出し元スクリプトから参照されるスクリプトの入力パラメーターに値をロードします。parameter フィールドは、参照されるスクリプトからの入力パラメーター変数を参照します。value フィールドは、その相関入力パラメーターに送信する値または呼び出し元スクリプトからの変数でなければなりません。 変数を使用する場合、この変数に格納されている値は、参照されるスクリプトの入力パラメーターに送信されます。 |
| 出力 | output |
Optional |
Text |
参照されるスクリプトからの出力パラメーターを呼び出し元スクリプト内の変数にバインドし、参照されるスクリプトが操作を終了するときに、出力パラメーターからの値を呼び出し元スクリプトの変数にロードします。parameter フィールドは、参照されるスクリプトからの出力パラメーター変数を参照します。value フィールドは、呼び出し元スクリプト内の変数を参照します。 |
| バージョン | version |
Optional |
Number |
実行するスクリプトのバージョンを指定します。 実動バージョンを参照するには、空のままにします。 version は、 file が無効になっている場合にのみ設定できます。versionを設定すると、Bot Runtime はパフォーマンスを向上させるためにスクリプトをキャッシュに入れます。 それ以外の場合、Bot ランタイムは実行のたびにスクリプトをダウンロードします。 |
出力パラメーター
| 「デザイナー」モードのラベル | 「スクリプト」モードの名前 | 使用可能な変数タイプ | 説明 |
|---|---|---|---|
| 成功 | value |
Boolean |
参照されたスクリプトの操作の状況を返します。 True 成功した場合は False 、操作が失敗した場合は。このパラメーターは、 「エラーの処理」を有効にした場合にのみ使用できます。 スクリプトが失敗した場合は、 Trueを返します。 エラー・メッセージ、 エラー行番号、 エラー・ルーチン名 、および エラー 出力パラメーターで取得した変数を確認して、呼び出されたスクリプトの実行中に発生した問題を識別します。 |
| エラー・メッセージ | errormessage |
Text |
参照されたスクリプトが失敗した場合に発生したエラー・メッセージを返します。 このパラメーターは、 「エラーの処理」を有効にした場合にのみ使用できます。 |
| エラー行番号 | linenumber |
Number |
参照されたスクリプトが失敗した場合にエラーが発生した行番号を返します。 このパラメーターは、 「エラーの処理」を有効にした場合にのみ使用できます。 |
| エラー・ルーチン名 | errorsubname |
Text |
参照されたスクリプトが失敗した場合にエラーが発生したサブルーチン名を返します。 このパラメーターは、 「エラーの処理」を有効にした場合にのみ使用できます。 |
| エラー | error |
Error |
参照されたスクリプトが失敗した場合にエラー・イベントの全詳細を返します。 このパラメーターは、 「エラーの処理」を有効にした場合にのみ使用できます。 |
例:
例 1
コーヒー・ショップを所有していて、クライアントとアイテムによる注文を追跡したいとします。 データベースには、以下の 4 つの表が必要です。
- クライアント: この表には、クライアントに関する情報 (クライアントの名前や E メール・アドレスなど) が保管されます。
- 注文: この表には、注文 ID やクライアント ID など、注文に関する情報が保管されます。
- 商品: この表には、商品 ID、商品の名前、および価格など、商品に関する情報が保管されます。
- オーダー・アイテム: この表には、オーダー ID や商品 ID など、オーダーごとのアイテムに関する情報が保管されます。
ショップで最も多くの金額を支払ったクライアントの名前と合計購入金額を取得するためのタスクが提供されます。
このデータベースを作成、取り込み、および照会するには、必要に応じてスクリプトのロジックを異なるファイルに分離し、 「スクリプトの実行」 コマンドを使用してメイン・スクリプトから各スクリプトを呼び出すことができます。
この例を実行するには、4 つの別個の WAL スクリプト・ファイルを作成し、それらをデスクトップに配置します。
connectToDb.wal:
defVar --name databasePath --type String --parameter --required
defVar --name sqliteConnection --type DbConnection --output
defVar --name connectionSuccess --type Boolean
defVar --name pathExists --type Boolean
defVar --name path --type String
ifFile --file "${databasePath}" pathExists=value
if --left "${pathExists}" --operator "Is_True"
sqliteConnect --connectionString "Data Source=\"${databasePath}\";Version=3;UseUTF16Encoding=True;" sqliteConnection=connection
else
sqliteConnect --createNew --path "${databasePath}" sqliteConnection=connection connectionSuccess=success
sqlExecute --connection ${sqliteConnection} --statement "CREATE TABLE IF NOT EXISTS clients (client_id INTEGER PRIMARY KEY AUTOINCREMENT,first_name TEXT NOT NULL,last_name TEXT NOT NULL,email TEXT NOT NULL);"
sqlExecute --connection ${sqliteConnection} --statement "CREATE TABLE IF NOT EXISTS products (product_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,price REAL NOT NULL);"
sqlExecute --connection ${sqliteConnection} --statement "CREATE TABLE IF NOT EXISTS orders (order_id INTEGER PRIMARY KEY AUTOINCREMENT,client_id INTEGER,FOREIGN KEY(client_id) REFERENCES clients(client_id));"
sqlExecute --connection ${sqliteConnection} --statement "CREATE TABLE IF NOT EXISTS order_items (order_item_id INTEGER PRIMARY KEY AUTOINCREMENT,order_id INTEGER,product_id INTEGER NOT NULL,quantity INTEGER NOT NULL,FOREIGN KEY(order_id) REFERENCES orders(order_id),FOREIGN KEY(product_id) REFERENCES products(product_id));"
endIf
checkDb.wal:
defVar --name databaseConnection --type DbConnection --parameter --required
defVar --name result --type Numeric --output
sqlExecuteScalar --connection ${databaseConnection} --statement "SELECT COUNT(*) FROM orders;" result=value
insertData.wal:
defVar --name databaseConnection --type DbConnection --parameter --required
sqlExecute --connection ${databaseConnection} --statement "INSERT INTO clients (first_name, last_name, email)\r\nVALUES\r\n(\'John\', \'Doe\', \'john.doe@example.com\'),\r\n(\'Jane\', \'Doe\', \'jane.doe@example.com\'),\r\n(\'Peter\', \'Parker\', \'peter.parker@example.com\'),\r\n(\'Bruce\', \'Wayne\', \'bruce.wayne@example.com\'),\r\n(\'Clark\', \'Kent\', \'clark.kent@example.com\');"
sqlExecute --connection ${databaseConnection} --statement "INSERT INTO products (name, price)\r\nVALUES\r\n(\'Espresso\', 3.99),\r\n(\'Latte\', 4.99),\r\n(\'Capuccino\', 5.99),\r\n(\'Macchiato\', 4.50),\r\n(\'Americano\', 3.50),\r\n(\'Iced coffee\', 4.50),\r\n(\'Cold brew coffee\', 5.00),\r\n(\'Frappuccino\', 6.00),\r\n(\'Mocha\', 5.50),\r\n(\'White chocolate mocha\', 6.00);"
sqlExecute --connection ${databaseConnection} --statement "INSERT INTO orders (client_id)\r\nVALUES\r\n(1),\r\n(2),\r\n(3),\r\n(4),\r\n(5);"
sqlExecute --connection ${databaseConnection} --statement "INSERT INTO order_items (order_id, product_id, quantity)\r\nVALUES\r\n(1, 1, 2),\r\n(1, 2, 1),\r\n(2, 3, 3),\r\n(2, 4, 1),\r\n(3, 5, 2),\r\n(3, 6, 1),\r\n(4, 7, 3),\r\n(4, 8, 1),\r\n(5, 9, 2),\r\n(5, 10, 1);"
queryDb.wal:
defVar --name databaseConnection --type DbConnection --parameter --required
defVar --name dataTable --type DataTable --output
sqlExecuteReader --connection ${databaseConnection} --statement "SELECT first_name, last_name, SUM(quantity * products.price) AS total_spent\r\nFROM clients\r\nJOIN orders ON clients.client_id = orders.client_id\r\nJOIN order_items ON orders.order_id = order_items.order_id\r\nJOIN products ON order_items.product_id = products.product_id\r\nWHERE products.price IS NOT NULL\r\nGROUP BY clients.client_id\r\nORDER BY total_spent DESC\r\nLIMIT 1;" dataTable=value
databaseConnection」変数が空であるため、エラーが発生します。これらのスクリプトを使用するには、以下のスクリプトを作成します。
defVar --name databasePath --type String
defVar --name path --type String
defVar --name sqliteConnection --type DbConnection
defVar --name result --type Numeric
defVar --name dataTable --type DataTable
defVar --name firstName --type String
defVar --name lastName --type String
defVar --name totalSpent --type Numeric
getSpecialFolder --folder "Desktop" path=value
setVar --name "${databasePath}" --value "${path}\\clients.db"
// Connects to database. Creates the database file if it doesn't exist.
executeScript --name "${path}\\connectToDb.wal" --parameters "{\"databasePath\":\"${databasePath}\"}" --output "{\"sqliteConnection\":\"${sqliteConnection}\"}"
// Checks if the database is populated.
executeScript --name "${path}\\checkDb.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"result\":\"${result}\"}"
// If the database is not populated, then it inserts data into the database. This avoids data duplication.
if --left "${result}" --operator "Greater_Than" --right 0 --negate
//Inserts data into the database. It only does that if the database has no data.
executeScript --name "${path}\\insertData.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}"
endIf
// Gets the client that spent the highest amount in your coffee shop, with the amount spent.
executeScript --name "${path}\\queryDb.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"dataTable\":\"${dataTable}\"}"
mapTableRow --dataTable ${dataTable} --row 1 --mappings "[{\"Name\":\"first_name\",\"Number\":\"1\",\"Output\":\"${firstName}\"},{\"Name\":\"last_name\",\"Number\":\"2\",\"Output\":\"${lastName}\"},{\"Name\":\"total_spent\",\"Number\":\"3\",\"Output\":\"${totalSpent}\"}]"
// Prints the result of the query.
logMessage --message "${firstName} ${lastName} has spent a total of US$ ${totalSpent} in your coffee shop." --type "Info"
// The output is: "Jane Doe has spent a total of US$ 22.47 in your coffee shop."
このメイン・スクリプトは、他のすべてのスクリプトを呼び出して、以下のタスクを実行します。
- データベースに接続します。 データベースが存在しない場合は、デスクトップに
clients.dbファイルが作成されます。 データベース接続変数はconnectToDb.walスクリプトによって返され、同じデータベースに接続するために他のスクリプトによって使用されます。 - データベースにデータが取り込まれているかどうかを検査します。
- データベースにデータが取り込まれていない場合は、データベースにデータが挿入されます。
- データベースを照会して、ショッピングで最も多くの金額を費やしたクライアントを取得します。 これは、クライアントの名前と合計消費量を返します。
- クライアントの名前と合計消費量を表示します。
例 2
実行時にエラーが発生した場合は、 Handle Error パラメーターを使用してエラー・メッセージを取り込むことができます。 このパラメーターが有効になっている場合、実行時に取り込まれるエラーはメイン・スクリプトに影響しません。 エラーは基本的に無視され、スクリプトは正常に終了するまで続行されます。 取り込まれたエラー・メッセージは、後でログに記録して分析することができます。
コーヒー・ショップに Espresso Latte という名前の新製品があり、新製品の売上を追跡したいとします。 これを行うには、既存のスクリプトを少し変更する必要があります。 デスクトップに espressoLatteQuery.wal ファイルを作成します。
espressoLatteQuery.wal:
defVar --name dbConnection --type DbConnection --parameter
defVar --name result --type String --output
sqlExecuteScalar --connection ${dbConnection} --statement "SELECT SUM(order_items.quantity) FROM AS espresso_latte_quantity FROM order_items JOIN products ON order_items.product_id = products.product_id WHERE products.name = \"Espresso Latte\";" result=value
次に、 executeScript.wal ファイルを編集します。
executeScript.wal:
defVar --name databasePath --type String
defVar --name path --type String
defVar --name sqliteConnection --type DbConnection
defVar --name result --type Numeric
defVar --name resultLatte --type String
getSpecialFolder --folder "Desktop" path=value
setVar --name "${databasePath}" --value "${path}\\clients.db"
// Connects to database. Creates the database file if it doesn't exist.
executeScript --name "${path}\\connectToDb.wal" --parameters "{\"databasePath\":\"${databasePath}\"}" --output "{\"sqliteConnection\":\"${sqliteConnection}\"}"
// Checks if the database is populated.
executeScript --name "${path}\\checkDb.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"result\":\"${result}\"}"
// If the database is not populated, then it inserts data into the database. This avoids data duplication.
if --left "${result}" --operator "Greater_Than" --right 0 --negate
//Inserts data into the database. It only does that if the database has no data.
executeScript --name "${path}\\insertData.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}"
endIf
// Gets the client that spent the highest amount in your coffee shop, with the amount spent.
executeScript --name "${path}\\espressoLatteQuery.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"result\":\"${resultLatte}\"}"
// Prints the result of the query.
logMessage --message "A total of ${resultLatte} Espresso Lattes have been sold in your coffe shop." --type "Info"
ただし、データベースには新製品に関連するデータがなく、 executeScript.wal ファイルを実行しようとしても何も返されず、「合計 Espresso Lattes がコーヒー・ショップで販売されました」というメッセージのみが表示されます。 スクリプトは失敗しません。より複雑な状況では、明示的なエラー・メッセージがないと、解決策を見つけるのが困難な問題になる可能性があります。
この例では、エラー処理の能力と、エラーをキャプチャーして IBM RPAで処理する方法を示すために、 Throw Exception (failTest) コマンドを使用して「espressoLatteQuery.wal」でエラーを発生させ、照会に問題があったことを明示することができます。
defVar --name dbConnection --type DbConnection --parameter
defVar --name result --type String --output
defVar --name errorMessage --type String --value "No Espresso Latte data found!"
defVar --name hasResult --type Boolean
sqlExecuteScalar --connection ${dbConnection} --statement "SELECT SUM(order_items.quantity) FROM AS espresso_latte_quantity FROM order_items JOIN products ON order_items.product_id = products.product_id WHERE products.name = \"Espresso Latte\";" result=value hasResult=hasresult
if --left "${hasResult}" --operator "Is_True" --negate
failTest --message "${errorMessage}"
endIf
ここで executeScript.wal を実行しようとすると、ボットによるタスクの完了を停止するエラー・メッセージが表示されます。 エラー・メッセージをキャプチャーできるようにするには、 Handle error パラメーターを有効にする必要があります。
defVar --name databasePath --type String
defVar --name path --type String
defVar --name sqliteConnection --type DbConnection
defVar --name result --type Numeric
defVar --name success --type Boolean
defVar --name errorMessage --type String
defVar --name resultLatte --type String
defVar --name errorLineNumber --type Numeric
defVar --name errorRoutine --type String
defVar --name error --type Error
getSpecialFolder --folder "Desktop" path=value
setVar --name "${databasePath}" --value "${path}\\clients.db"
// Connects to database. Creates the database file if it doesn't exist.
executeScript --name "${path}\\connectToDb.wal" --parameters "{\"databasePath\":\"${databasePath}\"}" --output "{\"sqliteConnection\":\"${sqliteConnection}\"}"
// Checks if the database is populated.
executeScript --name "${path}\\checkDb.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"result\":\"${result}\"}"
// If the database is not populated, then it inserts data into the database. This avoids data duplication.
if --left "${result}" --operator "Greater_Than" --right 0 --negate
//Inserts data into the database. It only does that if the database has no data.
executeScript --name "${path}\\insertData.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}"
endIf
// Gets the client that spent the highest amount in your coffee shop, with the amount spent.
executeScript --handleError --name "${path}\\espressoLatteQuery.wal" --parameters "{\"databaseConnection\":\"${sqliteConnection}\"}" --output "{\"result\":\"${resultLatte}\"}" success=value errorMessage=errormessage errorLineNumber=linenumber errorRoutine=errorsubname error=error
// Prints the result of the query.
if --left "${success}" --operator "Is_True" --negate
setVar --name "${resultLatte}" --value 0
endIf
logMessage --message "A total of ${resultLatte} Espresso Lattes have been sold in your coffe shop." --type "Info"
スクリプトは失敗しなくなり、呼び出されたスクリプトのいずれかでエラーが発生した後でも実行を継続します。 IBM RPA Studioの最後の行にブレークポイントを追加することによって、デバッグ・モードでエラー・メッセージを確認できるようになりました。また、ボットが IBM RPA Control Centerで公開されている場合は、ボットの詳細でエラー変数を確認できます。 この例では、スクリプトはエラー・メッセージを無視し、「espressoLatteQuery.wal」スクリプトが正常に実行されたかどうかを検査します。 正しくない場合は、正しくない出力を回避するために、値「0」を resultLatte 変数に割り当てます。