script include to get group members

levino
Giga Guru

Hi there

i have a reference field sys_user_group, i need to populate the user display name in a free text field 

 

I found a script include which is supposed to do it , not sure if its the correct one

getGroupMembers: function() {
var grpID = this.getParameter('sysparm_group');
var memObj = new GlideRecord('sys_user_grmember');
memObj.addQuery('group',grpID);
memObj.query();
var nArray = [];
var vArray = [];
while(memObj.next(){
	nArray.push(memObj.user.getDisplayValue().toString();
	vArray.push(memObj.getValue("user"));
})
var retValue = nArray.toString() + '|' + vArray.toString();
return vArray;
},

//onchange client script  -  sys_user_group variable

var grmember = new GlideAjax('restrictduplicateitem');
add.parm('sysparam_name','getGroupMembers');
add.parm('sysparm_gid','newValue');
ga.getXML(callback);

function callback(response)
{
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue("existing_members", answer);
}

 

1 ACCEPTED SOLUTION

Hi @levino ,

 

1)Right click on the existing member field 

HemanthM1_0-1689154366614.png

and check read only (so that it will be read only all the time and no one edit

HemanthM1_1-1689154426326.png

 

Accept and Hit Helpful if it resolves.

 

Thank you,

Hemanth

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

4 REPLIES 4

Hemanth M1
Giga Sage
Giga Sage

Hi @levino ,

 

You need update your script:

here you go:

Script include :

    getGroupMembers: function() {
        var grpID = this.getParameter('sysparm_gid');
        var memObj = new GlideRecord('sys_user_grmember');
        memObj.addQuery('group', grpID);
        memObj.query();
        var nArray = [];
        var vArray = [];
        while (memObj.next()) {
            nArray.push(memObj.user.getDisplayValue().toString());
            //vArray.push(memObj.getValue("user"));
        }
        //var retValue = nArray.toString() + '|' + vArray.toString();
        return nArray.toString();

    },

Client script:

  //Type appropriate comment here, and begin script below
    var grmember = new GlideAjax('restrictduplicateitem');
    grmember.addParam('sysparm_name', 'getGroupMembers');
    grmember.addParam('sysparm_gid', newValue);
    grmember.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue("existing_members", answer);
    }

Script include example :

HemanthM1_0-1689097809487.png

Client script example on incident:

HemanthM1_1-1689097864608.png

Result :

HemanthM1_2-1689097897043.png

 

Accept and Hit Helpful if it resolves.

 

Thank you,

Hemanth

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hemanth M1
Giga Sage
Giga Sage

Hi @levino ,

 

Close the thread by accepting the solution if it works so that if someone wants the same solution they can refer.

 

Thank you,

Hemanth 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Thanks so Hemanth

work well,  just one query how can i set the value to read-only

Hi @levino ,

 

1)Right click on the existing member field 

HemanthM1_0-1689154366614.png

and check read only (so that it will be read only all the time and no one edit

HemanthM1_1-1689154426326.png

 

Accept and Hit Helpful if it resolves.

 

Thank you,

Hemanth

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025