2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ServiceNow 動的フィルターを作成

2
Last updated at Posted at 2026-11-15

ServiceNowでは、再利用できる動的フィルターを設定できます。
その使用例やカスタムのフィルターの実装方法を紹介します。

標準機能の動的フィルター

例えば、自分が所属する全てのグループの資産を絞り込み条件として設定したい場合、
alm_assetテーブルのリストでSupport group, is(dynamic), One of My Groupsを選択すると、
該当の資産が表示されます。
image.png

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

カスタマイズの例

標準機能のOne of My Groupsでは、あくまで自分のグループのみが絞り込まれます。
例えば、親子関係のあるグループも設定したい場合、カスタマイズが必要です。
image.png
今回は下記の3パターンを紹介します。
※Groupテーブルに作成したカスタマイズのu_main_groupという項目を基に絞り込みます

  1. 自分のメインのグループと直接の子グループ
  2. 自分のメインのグループと子グループの子グループも含めた配下の全てのグループ
  3. 親グループも含めた親子グループ
    (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
image.png

Newボタンを押し、各項目を入力します。

項目 値
Label 絞り込み条件の選択肢として表示する名前
Script Script Includeのスクリプト名と関数名
Field type Reference
Referenced table 選択肢の参照先テーブル
Available for filter true

image.png

実装結果

追加したフィルターが選択肢に表示されるようになります。
image.png

  1. My Group and Child Groups
    自分のメインのグループと直接の子グループ
    image.png

  2. My Group and All Descendant Groups
    自分のメインのグループと子グループの子グループも含めた配下の全てのグループ
    image.png

  3. My Group and All Parent and Child Groups
    親グループも含めた親子グループ
    image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?