別の変数で指定したグループに所属するユーザーのみを参照させたい。

NaoyaT
Tera Expert

前回の質問に引き続き、参照変数についてご教示いただきたいです。

 

参照変数(A、B)を用意し、Aはsys_user_group、Bはsys_userを参照するようにしています。

BはAで指定したグループに所属するユーザーのみを参照させたいです。

実現させるためにはカタログクライアントスクリプトで、Aの値を取得し、Bのフィルター条件に挿入するイメージですが、スクリプトの設定方法をご教示いただきたいです。

現在のカタログクライアントスクリプトは以下の通りです。

UI Type:All
type:onchange

Variable name:A

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var grname = g_form.getValue('B');

    var query;
    var grmem = new GlideRecord('sys_user_grmember');
    grmem.addQuery('group.name', grname);
    grmem.query();

    var usrIDs = [];
    while (grmem.next()) {
        usrIDs.push(grmem.user.sys_id.toString());
    }
    query = 'sys_idIN' + usrIDs.join(',');
    query;

    g_form.setValue('A', query);
}

 

2 件の受理された解決策

Ankur Bawiskar
Tera Patron
Tera Patron

@NaoyaT 

your onChange is on which variable and what is the type?

I assume your variable A is referring to Group table and variable B is referring to User table

if yes then update as this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '')
        g_form.clearValue('B');

    var grname = g_form.getValue('A');

    var grmem = new GlideRecord('sys_user_grmember');
    grmem.addQuery('group', grname);
    grmem.query(function(gr) {
        var usrIDs = [];
        while (grmem.next()) {
            usrIDs.push(grmem.user.sys_id.toString());
        }
        // You can now use usrIDs array as needed
        g_form.setValue('B', usrIDs.toString());
    });

}

If the group variable holds group name then simply update this line

    grmem.addQuery('group.name', grname);
 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

元の投稿で解決策を見る

@Ankur Bawiskar 

Thank you for your response.

 

My apologies. It was my lack of knowledge.

I had assumed that getting a value from another reference variable was done using a catalog client script,
I have confirmed that it is also possible to set a filter on the reference variable.
Here is what I was able to implement

■[reference]-[Type Specifications]
・Reference:User[sys_user]
・Use reference qualifier:Advanced
・Reference qualifier:

javascript:
var query;
var value = current.variables.A;
 
var grmem = new GlideRecord('sys_user_grmember');
grmem.addQuery('group', value);
grmem.query();
 
var usrIDs = [];
 
while (grmem.next()) {
    usrIDs.push(grmem.user.sys_id.toString());
}
query = 'sys_idIN' + usrIDs.join(',');
query;

※"A" is referring to Group variable.

元の投稿で解決策を見る

6件の返信6

@Ankur Bawiskar 

Sorry, I made a mistake.
I redid the answer selection.

@NaoyaT 

Glad to know that my script helped.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader