LoginSignup
8
9

More than 3 years have passed since last update.

ServiceNowで使用するJavaScript@1_5まで

Last updated at Posted at 2019-07-15

はじめに

今回の動画は前回の記事の内容編となります。
内容は表題の通り「ServiceNow上でのJavaScript」です。投稿範囲は下記です。

No タイトル リンク(Qiita記事リンク)
0 Overview 未投稿
1 Getting Started 未投稿
2 Statements and syntax 未投稿
3 Variables 未投稿
4 Simple Arithmetic Operators 未投稿
5 Common Error Messages 未投稿

0.Overview:概要

動画の投稿者:Chuck Tomasi
この動画の取り扱い範囲:
servicenowでのscriptの全般。言語的にはJavaScriptで、ServiceNow特有のことも取り扱う。
リソースサイト:
bit.ly/sn-js

1.Getting Started

1_1.js
// アラートログを発生させる
gs.info('Hello, world!');
1_2.js
// サーバサイドスクリプト
var gr = new GlideRecord('task');
gr.addActiveQuery();
gr.query();

while (gr.next()) {
    gs.info(gr.getValue('number'));
}
// taskテーブルの中身
// task,incident,problem,change requests,approval,
1_3.js
// クライアントサイドスクリプト
function onLoad() {
  alert('Current state value is: ' + g_form.getValue('state'));
}

1.開発環境を作ろう
ServiceNowでは個人用インスタンス(PDI)が用意されている。
登録すれば無料で利用できるので登録しよう。基本的な開発はPDIで行う。
登録方法はQiitaの他記事参照のこと。
また、2019.07.15現在の開発バージョンはMadridです。

2.開発と実行
下記、ログイン後画面
FireShot Capture 014 - System Administration - ServiceNow - dev74512.service-now.com.png
ログイン後画面の左メニューから、下記を入力することで実行環境(サーバ)にアクセスできます。
scripts >background > scripts background
FireShot Capture 015 - ServiceNow - dev74512.service-now.com.png
ここで、1_1.jsなどを実行できます。

3.ServiceNowのJavaScript環境の特徴
ブラウザ上でサーバサイドスクリプトとクライアントサイドスクリプトの実行が可能なところ。
サーバ:DBへの作成更新削除取得などの動作
クライアント:画面ロードや値変更時のブラウザ処理

サーバスクリプトの実例は、上記1_2.js
クライアントスクリプトの実例は、上記1_3.js

4.クライアントスクリプトの開発と実行
ログイン後画面の左メニューから、下記を入力することで実行環境(クライアント)にアクセスできます。
system definition > client scripts

ここでNewで新しいスクリプトを書いて実行させることができます。
対象テーブル:incident
実行タイミング:on load
スクリプト:1_3.js

5.追加情報
servicenowの実装の不明点があればcommnunityに質問しよう。
JavaScriptの言語仕様はcode academyで学習しよう。

2.Statements and syntax:構文

本稿のスクリプト

2_1.js
// セミコロンのシンタックス
// Required:
var i = 0; i++        // <-- semicolon obligatory
                      //     (but optional before newline)
var i = 0             // <-- semicolon optional
    i++               // <-- semicolon optional

// Optional:
var i;                        // variable declaration
i = 5;                        // value assignment
i = i + 1;                    // value assignment
i++;                          // same as above
var x = 9;                    // declaration & assignment
var fun = function() {...};   // var decl., assignmt, and func. defin.
alert("hi");                  // function call

// Avoid
// NO semicolons after }:
if  (...) {...} else {...}
for (...) {...}
while (...) {...}

// BUT:
do {...} while (...);

// function statement: 
function (arg) { /*do this*/ } // NO semicolon after }

// Exception:
for (var i=0; i < 10; i++)  {/*actions*/}       // correct
for (var i=0; i < 10; i++;) {/*actions*/}       // SyntaxError

Javascriptの基本文法
環境:scripts background
内容:
var キーワードで宣言
; セミコロンで終わりを明示
if 文でシナリオの分岐とエラー処理
do-while 文で一回実行
function の終わりにはセミコロンは不要
for 文のconditionで使用する

(上記、JavaScriptの基本文法の復習につき、他記事参照のこと)

3.Variables:変数

3_1.js
// L03S01 - Simple variables & good/bad variable names
//
var name   = 'Chuck';   // Simple string variable
var i      = 0;         // Simple integer variable
var answer = true;      // Simple boolean variable

// Naming
var c = "http://www.amazon.com";  // Not ideal
var case = 'CASE0010001';         // Reserved word!
var lastEntryInTheListWithRelatedRecords = true; // a bit long?
// personCount      // Indicates a counter/integer
// personList       // Must be a list
// personObj        // Object
// personGr         // GlideRecord

Nowでの型で基本的に使うもの
1.String
2.Integer
3.Boolean

変数の命名規約
上記のコードにおける変数名caseは、短いので修正
case->caseNumber
キャメルケースで、かつ長すぎないもの。シンプル過ぎないものが望ましい。
また、..Count,..List,..Obj,..Grなどのデータタイプを示したものが良い。

4.Simple Arithmetic Operators:算術記号

4_1.js
// L04S01 - Mathematical operators
//

// Assignment
var a = 12;
var b = 3;

// Addition
gs.info(2 + 2);
gs.info(a + 2);
b = b + 2;
// b += 2;  // shorthand for the line above
gs.info(a + b);

// Increment by 1
a++;
gs.info(a);

// Decrement by 1
b--;
gs.info(b);

// Multiply
gs.info(a * b);

// Division
gs.info(a / b);

// Modulo - get the remainder of a division
gs.info('');
gs.info(a);
gs.info(b);
gs.info(a % b);

var c = (5 + 4) * 2;
gs.info(c);

JavaScriptには5つの算術オペレーターが存在する。
1.+increment
2.-decrement
3.xmultiple
4./division
5.%modulo(*乗算のあまり)

5.Common Error Messages:エラー対処

5_1.js
// L05S01 - Common error messages
//
gs.info(myUnknownVariable);
ga.info('Hello, world!);

上記のコードは実行時エラーが存在する。なのでそのエラーを解消するタスク。
下記、エラーメッセージとその対処である。
1.underterminated string lieterals
->文字列をクオートで閉じること
2.'myUnknownVariable' is not defined.
->変数が宣言されていない。これを宣言する。
var myUnknownVariable = 'Chuck';
3.'ga' is not defined.
ga->gs

完成例文.js
// L05S01 - Common error messages
//
var myUnknownVariable = 'Chuck';
gs.info(myUnknownVariable);
gs.info('Hello, world!');
8
9
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
8
9