Displaying User Count Works on Scripts - Background but not on Client Side

GeorgeConsS
Tera Contributor

I am encountering an issue while attempting to retrieve the number of users in the "Network" group using a Script Include and GlideAjax in ServiceNow. Despite implementing the logic to query the sys_user_group and sys_user_grmember tables, the response returned to the client script is null.


Steps Taken:
Created a Script Include (GetNetworkGroupUserCount) that queries the "Network" group and counts users.
Verified the exact group name in sys_user_group and confirmed users exist in sys_user_grmember.
Checked logs (System Logs > All), but the response in the client script remains null.
Ensured the Script Include is marked as "Client Callable" and properly formatted for GlideAjax.

Current Issue:
The expected value should be the total number of users in the group, but the client-side script is receiving null instead.
Debugging via gs.info() in the Script Include shows the correct count in logs, yet it does not return properly to GlideAjax.
Would you be able to provide insights into why the response is not being retrieved correctly? Any suggestions on debugging or possible misconfigurations would be greatly appreciated.

Thanks in advance for your help!

2 REPLIES 2

Medi C
Giga Sage

Hi @GeorgeConsS,

 

Could you please share screenshots from your script include and your client script? 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Hello here's the scripts for each

Script Includes:

var GetNetworkGroupUserCount = Class.create();
GetNetworkGroupUserCount.prototype = {
initialize: function() {},

getUserCount: function() {
var count = 0;
var group = new GlideRecord('sys_user_group');
group.addQuery('name', 'Network');
group.query();

if (group.next()) {
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('group', group.sys_id);
userGR.query();

while (userGR.next()) {
count++;
}
}

gs.info('Network Group User Count: ' + count);
return count.toString();
},

type: 'GetNetworkGroupUserCount'
};

Client Script:
function onLoad() {
var userCount = new GlideAjax('GetNetworkGroupUserCount');
userCount.addParam('sysparm_name', 'getUserCount');
 
userCount.getXMLAnswer(function(response) {
if (response && response !== "null") {
g_form.addInfoMessage('Number of users in Network group: ' + response);
} else {
g_form.addInfoMessage('Failed to retrieve Network group user count. Check Script Include or group name.');
}
});
}