3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ServiceNowでBusinessRuleでレコード挿入時に金額を自動計算して設定する

Last updated at Posted at 2018-04-13

概要

単価(UnitPrice)と数量(Quantity)を持つテーブルを作成する。
Business Rule でテストにレコード登録前に、単価と数量から金額を算出して、金額(Price)に設定する。Bisiness Ruleで金額計算は、Script Includeで作成したAPIを起動する。
これらの一連の処理を、ServiceNowのAutomated Test Frameworkを使ってテストを実施する。

環境

ServiceNow:KINGSTONE

準備

テーブルを作成する

新規テーブルをLabel = "Sales"、 Name = "x_211750_angular_a_sales"(自動生成)作成し、下記の3つのフィールドを追加する。

Column label Column Name Type length
UnitPrice unitprice Currency 20
Quantity quantity Integer 10
Price price Currency 20

Script Includeで金額計算APIを作成する

Studioから「Create Application File」で「Server Development」カテゴリーから「Script Include」を選択してCreateをクリック。
Name = "CalcPrice"でScriptには下記を入力する。

var CalcPrice = Class.create();
CalcPrice.prototype = {
    initialize: function() {
    },
	calc:function(unitprice, quantity) {
		gs.info('service include script called!');
		return unitprice * quantity;
	},

    type: 'CalcPrice'
};

単に単価と数量を乗じてその結果を返却する関数だ。

Business Ruleを作成する

Saleテーブルに挿入前に金額を自動設定するBusinessRuleを作成する。
Studioの「Create Application File」で、「Server Development」カテゴリの「Business Rule」を選択して、Createをクリックする。
設定画面で下記の通り設定する。

項目名
Name CalcPrice
Table x_211750_angular_a_sales
Active チェックする
Advanced チェックする
When before
Insert チェックする

Advanced タブのscriptに次の入力をする。

(function executeRule(current, previous /*null when async*/) {

	gs.info('called before insert to Sales');
	var quantity =current.getValue('quantity');
	var unitPrice = current.getValue('unitprice');
	
	var calculator = new CalcPrice();
	
	var price = calculator.calc(unitPrice,  quantity);
	
	current.setValue('price', price);
	

})(current, previous);

ここまでで準備が完了だ。

テスト作成

テストファーストという考え方からすると、テストを作成してから、テストを実行しテスト結果がNGとなる事を確認してから、テストがOKとなるようにソースコードを作成するのが筋だが、ここでは動作するコードを書いてからテストを作成、実施している。

テストは、Studioではなくアプリケーションブラウザーから作成する。
Automated Test Frameworkから「Tests」を選択して、「New」で新規テストを作成する。

まずは、Script IncludeのCalcPriceのテストコードから作成する。
テスト設定画面の「Add Test Step」でテストステップを追加する。
「Server」カテゴリの「Run Server Side Script」を選択してNextをクリックする。

Test Step設定画面の最初から入力されているScriptを削除して次のように入力する。

(function(outputs, steps, stepResult, assertEqual) {
    // add test script here
	var calculator = new CalcPrice();
	
	var price = calculator.calc(12.5, 2);
	
	assertEqual({name: "task gr exists", shouldbe: 25, value: price});


})(outputs, steps, stepResult, assertEqual);

なお、最初から入力されているScriptはほとんどがコメントでコメント内にテストの作成方法が記述されている。

Script Includeのテストの実行

テストを実行するにはテスト設定画面の「Run Test」をクリックするだけだ。
ScriptIncludeRunTest.png

今回はテスト成功しているが、テストNGとなる場合も見ておいた方が良いだろう。

ScriptIncludeTestResult.png

Business Ruleのテスト

Salesテーブルにレコードを追加して、Priceが自動計算されて設定される事をテストで確認する。
新規にテストを作成し、「Add Step」でテストを追加し、「Server」カテゴリの「Record Insert」を選択して「Next」をクリックする。
次の設定で追加する。

RecordInsert.png

さらに「Add Test」でテストを追加し、「Server」カテゴリの「Record Query」を選択して「Next」をクリックする。
次の設定で追加する。
RecordQuery.png

Price = 16のレコードが取得可能かどうかをテストで確認実施する。

Business Ruleのテスト実行

テストを実行しテスト結果を確認する。
Application LogにSciprから出力したログが出力されていることも確認しておく。

BusinessRuletestResult.png

TestLog.png

参考

画面の自動テストはこちら。

ServiceNowでFormの自動テストを実施する方法

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?