GlideScopedEvaluator - スコープ対象
GlideScopedEvaluator API を使用すると、スコープ対象のサーバースクリプトとグローバルサーバースクリプトの両方から GlideRecord フィールドのスクリプトを評価できます。
この API は、スクリプトフィールドを定義してレコードを評価します。レコードのスコープによってスクリプトのスコープが定義されます。
GlideScopedEvaluator - evaluateScript(GlideRecord grObj, 文字列 scriptField, オブジェクト variables)
GlideRecord フィールドに存在するスクリプトを評価します。
| 名前 | タイプ | 説明 |
|---|---|---|
| grObj | GlideRecord | スクリプト式を含む GlideRecord。 |
| scriptField | 文字列 | オプション。スクリプト式を含むフィールドの名前。 |
| variables | オブジェクト | オプション。名前と値のペアを持つ変数のマップ。これらの変数は、このメソッドの実行中にスクリプトで使用できます。 |
| タイプ | 説明 |
|---|---|
| オブジェクト | スクリプトの実行結果。 |
// Setting up a record that contains the script to be executed.
var now_GR = new GlideRecord('u_global_table');
now_GR.u_short_description = 'Calculate Addition';
now_GR.u_test_script = "result = x + y";
now_GR.insert();
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.evaluateScript(now_GR, 'u_test_script', null);
gs.info(evaluator.getVariable('result'));
出力:
300
GlideScopedEvaluator - getVariable(文字列 name)
GlideScopedEvaluator オブジェクトから指定された変数を返します。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 返される変数の名前。 |
| タイプ | 説明 |
|---|---|
| オブジェクト | 指定した変数の値。 |
次の例は、 getVariable() メソッドを呼び出して 回答 変数の値をチェックする方法を示しています。
(function executeRule(current, previous /*null when async*/) {
var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group
if (grAG.isValidRecord()) {
var ge = new GlideScopedEvaluator();
ge.putVariable("current", current); // Pass through the "current" variable as "current"
ge.putVariable("group", grAG); // Pass through the "grAG" variable as "group"
ge.putVariable("answer", true); // default "answer" to TRUE
ge.evaluateScript(grAG, "u_assignment_condition"); // Run the script
// Abort the transaction if the "answer" variable was set to FALSE explicitly (undefined doesn't count)
if (ge.getVariable("answer") === false) {
gs.addErrorMessage("Assignment rule did not pass");
current.setAbortAction(true);
}
}
})(current, previous);
GlideScopedEvaluator - GlideScopedEvaluator()
GlideScopedEvaluator オブジェクトをインスタンス化します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
GlideScopedEvaluator - putVariable(文字列 name, オブジェクト value)
GlideScopedEvaluator オブジェクトに変数を設定します。これらの変数は、この GlideScopedEvaluator オブジェクトが実行するスクリプトで使用できます。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 変数の名前。 |
| value | オブジェクト | 変数の値。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、 putVariable() メソッドを呼び出して answer 変数を true に設定する方法を示しています。
(function executeRule(current, previous /*null when async*/) {
var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group
if (grAG.isValidRecord()) {
var ge = new GlideScopedEvaluator();
ge.putVariable("current", current); // Pass through the "current" variable as "current"
ge.putVariable("group", grAG); // Pass through the "grAG" variable as "group"
ge.putVariable("answer", true); // Set default "answer" to TRUE
ge.evaluateScript(grAG, "u_assignment_condition"); // Run the script
// Abort the transaction if the "answer" variable was set to FALSE explicitly (undefined doesn't count)
if (ge.getVariable("answer") === false) {
gs.addErrorMessage("Assignment rule did not pass");
current.setAbortAction(true);
}
}
})(current, previous);