- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2020 05:19 AM
Hello All,
I am using a refernce qualifier on Group variable which works fine.
Now I want to count the "number of groups" that group variable is showing.If my group(s) count is one then I need to set the one group in the group field and it should be read only.if the group count is greater than 1 then user will select the groups.
How to achieve this?Below is my reference qualifier code:
Reference qualifier:javascript: new getgroupcontext().getgrpcontext(current.variables.role2);
var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontext : function(role)
{
//var role = this.getParameter('sysparm_query');
var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();
while(gr.next())
{
//gs.log("@362363"+gr.group+"NOWROLE"+gr.role.name);
groups.push(gr.group);
}
return 'sys_idIN' + groups.toString();
},
type: 'getgroupcontext'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 04:53 AM
Hi Kumar,
you cannot set value to group variable if script include returns 1 value; reference qualifier will help in restricting the records and not setting the value
you can use this approach:
1) use onChange client script and use GlideAjax and check if only 1 group exist with that role; if yes then return that and set the value; if more than 1 return sys_ids
Same function you can use for both the purpose; one from ref qualifier and one from GlideAjax
2) keep the reference qualifier as it is -> this will help users to see restricted groups if more than 1
Note: I assume the role2 variable is a string variable
Client Script: onchange client script on role variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == ''){
g_form.clearValue('group_variable');
return;
}
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroups");
ga.addParam('sysparm_role', newValue);
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('group_variable', answer);
}
});
}
Script Include:
var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontext : function(role)
{
var role = JSUtil.nil(role) ? this.getParameter('sysparm_role') : role;
var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();
if(gr.getRowCount() == 1)
return gr.getValue('group');
else{
while(gr.next())
{
groups.push(gr.getValue('group'));
}
return 'sys_idIN' + groups.toString();
}
},
type: 'getgroupcontext'
});
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2020 05:30 AM
Can you try something as below (untested).
var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontext : function(role)
{
//var role = this.getParameter('sysparm_query');
var countis='';
var x;
var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();
while(gr.next())
{
//gs.log("@362363"+gr.group+"NOWROLE"+gr.role.name);
countis=gr.getRowCount();
if(countis==1)
{
x=gr.group.sys_id;
}
else
{
groups.push(gr.group);
x='sys_idIN' + groups.toString();
}
return x;
},
type: 'getgroupcontext'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 12:55 AM
Hello Jaspal,
The below script with logs,giving the count as 2(which is expected),but while returning the groups sys_id it is giving only one sys_id instead of 2.Any idea?Attached log for reference.
If I have a rowcount of 1,I need to set the single group in the group variable by using client script(glideajax),how to achieve this?
var getgroupcontexttest = Class.create();
getgroupcontexttest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontexttestf : function(role)
{
//var role = this.getParameter('sysparm_query');
var countis='';
var x=[];
var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();
while(gr.next())
{
gs.log("@362363"+gr.group+"NOWROLE"+gr.role.name);
countis=gr.getRowCount();
gs.log("@362363 the COUNT is " + countis);
if(countis==1)
{
gs.log("@362363 the COUNT is only one ");
x=gr.group.sys_id;
//x='sys_idIN' + gr.group.sys_id;
gs.log("@362363 the value of X is " + x);
}
else
{
gs.log("@362363 the COUNT is more than one ");
groups.push(gr.group);
x='sys_idIN' + groups.toString();
}
gs.log("@362363 the last return of X is " + x);
return x;
}
},
type: 'getgroupcontexttest'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 01:15 AM
Can you replace else part with below in code above.
else
{
x=groups.push(gr.group.sys_id);
}
For populating it on the field with the value you need to write a client scirpt that would set it. Look for link for a check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 04:31 AM
Hi Jaspal,
I tried with multiple ways,but it seems not working.