0
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 1 year has passed since last update.

ServiceNow 日付フォーマットの変換方法

Posted at

本記事はServiceNowのDate型フィールドの値を、メール通知等でアウトプットする際、フォーマットを変換する際のスクリプト作成方法となります。

バージョンとUI情報は以下となります。
バージョン:Sandiego
UI:旧版(Quebec以前)より

日付フォーマット変換のコード
ServiceNowではデフォルトでDate形式は以下となっています。

yyyy-MM-dd hh : mm : ss

今回はこのフォーマットをyyyy年MM月dd日 h時m分のフォーマットに変換しています。
下方のコードはメール通知スクリプトより作成、メールを通知する際、開始日時(opend_at)フィールドを変換しています。

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

	var date = new GlideDateTime(current.opened_at);
	var date2 = date.getDate();
	var date3 = date2.getByFormat("yyyy年MM月dd日");
	
	var time = date.getTime();
	var h = time.getHourOfDayLocalTime();
	var m = time.getMinutesLocalTime();
	var s = time.getSeconds();

	template.print(date3+h+"時"+m+"分");
	
})(current, template, email, email_action, event);

GlideDateTime,GlideDateに関する詳細は以下を参照ください。
https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideDateTimeAPI#r_GDT-GlideDateTime

Appendix ServerSideScriptのログ出力・確認方法

合わせてServerSideScriptのログ確認方法をAppendixとして載せておきます。

gs.debug("debug");
gs.info("info");
gs.warn(""warn);
gs.error("error");

上記ログ出力を導入した場合、

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

	var date = new GlideDateTime(current.opened_at);
	var date2 = date.getDate();
	var date3 = date2.getByFormat("yyyy年MM月dd日");
	
	gs.info("date="+date);
	gs.info("date="+date2);
	gs.info("date="+date3);
	・・・

上記のように記載することで、スクリプト途中の値をログとして出力し、Debugを行うことが可能です。
ログの確認方法は以下となります。

手順1
アプリケーションナビゲータに"application log"と入力し、メニューより"Application log"を選択します。

Created
Level(info等)
Message(date3=yyyy年MM月dd日等)

を確認し、想定通りの処理、出力が行われているか確認可能です。

image.png

詳細は以下のリンク参照ください。
https://developer.servicenow.com/dev.do#!/learn/courses/quebec#scripting-in-servicenow

以上です。
今後もServiceNowのスクリプト関連に関しても事例を用いて投稿していきたいと思いますので宜しくお願いします。

0
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
0
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?