ServiceNowでは、再利用できる動的フィルターを設定できます。
その使用例やカスタムのフィルターの実装方法を紹介します。
標準機能の動的フィルター
例えば、自分が所属する全てのグループの資産を絞り込み条件として設定したい場合、
alm_assetテーブルのリストでSupport group, is(dynamic), One of My Groupsを選択すると、
該当の資産が表示されます。

また、カタログアイテムの変数の選択肢の絞り込み条件としても設定できます。
<変数の設定>

<カタログアイテムの画面>

カスタマイズの例
標準機能のOne of My Groupsでは、あくまで自分のグループのみが絞り込まれます。
例えば、親子関係のあるグループも設定したい場合、カスタマイズが必要です。

今回は下記の3パターンを紹介します。
※Groupテーブルに作成したカスタマイズのu_main_groupという項目を基に絞り込みます
- 自分のメインのグループと直接の子グループ
- 自分のメインのグループと子グループの子グループも含めた配下の全てのグループ
- 親グループも含めた親子グループ
(Child Group1とGhild Group2は配下のグループも含め、互いに表示しない)
実装方法
スプトインクルードで絞り込み条件を設定します。
| 項目 | 値 |
|---|---|
| Name | u_createDynamicFilter |
| Glide AJAX enabled | true |
| Sandbox enabled | true |
var u_createDynamicFilter = Class.create();
u_createDynamicFilter.prototype = {
initialize: function() {},
// 1. Returns the user's main group and its direct child groups
getMyGroupAndChildGroups: function() {
return this._getGroupHierarchy(false, false);
},
// 2. Returns the user's main group and all descendant groups
getMyGroupAndAllDescendantGroups: function() {
return this._getGroupHierarchy(false, true);
},
// 3. Returns the user's main group, all ancestor groups, and all descendant groups
getMyGroupAndAllAncestorAndDescendantGroups: function() {
return this._getGroupHierarchy(true, true);
},
// Retrieves the group hierarchy based on the specified options
_getGroupHierarchy: function(includeAncestors, includeDescendants) {
var groupIds = this._getMainGroupIds();
if (groupIds.length === 0) {
return groupIds;
}
var mainGroupId = groupIds[0];
if (includeAncestors) {
this._getParentGroups(mainGroupId, groupIds);
}
if (includeDescendants) {
this._getChildGroups(mainGroupId, groupIds, true);
} else if (!includeAncestors) {
this._getChildGroups(mainGroupId, groupIds, false);
}
return groupIds;
},
// Retrieves the user's main group ID
_getMainGroupIds: function() {
var groupIds = [];
var userGR = new GlideRecord('sys_user');
if (!userGR.get(gs.getUserID())) {
return groupIds;
}
var mainGroupId = userGR.getValue('u_main_group');
if (mainGroupId) {
groupIds.push(mainGroupId);
}
return groupIds;
},
// Retrieves parent groups recursively
_getParentGroups: function(groupId, groupIds) {
var groupGR = new GlideRecord('sys_user_group');
if (!groupGR.get(groupId)) {
return;
}
var parentGroupId = groupGR.getValue('parent');
if (!parentGroupId || groupIds.indexOf(parentGroupId) !== -1) {
return;
}
groupIds.push(parentGroupId);
this._getParentGroups(parentGroupId, groupIds);
},
// Retrieves child groups, optionally including all descendants
_getChildGroups: function(parentGroupId, groupIds, recursive) {
var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('parent', parentGroupId);
groupGR.query();
while (groupGR.next()) {
var childGroupId = groupGR.getUniqueValue();
if (groupIds.indexOf(childGroupId) !== -1) {
continue;
}
groupIds.push(childGroupId);
if (recursive) {
this._getChildGroups(childGroupId, groupIds, true);
}
}
},
type: 'u_createDynamicFilter'
};
②Dynamic Filter Optionsを作成します。
System Definition > Dynamic Filter Options

Newボタンを押し、各項目を入力します。
| 項目 | 値 |
|---|---|
| Label | 絞り込み条件の選択肢として表示する名前 |
| Script | Script Includeのスクリプト名と関数名 |
| Field type | Reference |
| Referenced table | 選択肢の参照先テーブル |
| Available for filter | true |




